简体   繁体   English

c++ srand(time(0)) 在猜谜游戏中不起作用

[英]c++ srand(time(0)) doesn't work in guessing game

I need to make a guessing game in C++, and everything works except that srand(time(0)) doesn't reset the number after the user wants to play again.我需要在 C++ 中做一个猜谜游戏,除了 srand(time(0)) 在用户想再次玩游戏后不会重置数字之外,一切正常。 I also can't use std libraries.我也不能使用标准库。

Nothing I have done has worked so far.到目前为止,我所做的一切都没有奏效。 Am I doing the while loops wrong?我在做while循环错了吗?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
  //Initialize variables
  int input = 0;
  int playing = 1;
  char yninput[2];
  int count = 1;
  //While the player is playing the game
  while (playing == 1) {
    srand(time(0));
    int num = rand() % 101;
    //While the player hasn't guessed the number
    while (input != num) {
     //Prompt the player
       cout << "Enter your guess" << endl;
      cout << num << endl;
      cin >> input;
      //If the guess is greater than the number
      if (input > num) {
    cout << " Your guess is too high!" << endl;
   count++;
      } 
      //If the guess is less than the number
      else if (input < num) {
    cout << " Your guess is too low!" << endl;
    count++;
      }
      //If the player guesses the correct number
      else {
    cout << " You have guessed the number! It took you " << count << " 
guess(es)! Would you like to play again?" << endl;
    //Ask the play if they want to play again
    cin >> yninput[2];
    //If the player doesn't want to play again quit the program
    if (yninput[2] == 'n') {
      playing = 0;
      input = num;
        }
    //If the player wants to play again restart the program and 
     randomize the number
    else if (yninput[2] == 'y') {
       input = 0;
      count = 1;
         }
      }
    }
  }
}

As @user4581301 has pointed out, you shouldn't call srand(time(0)) more than once, as it will reset the random seed according to the current system time.正如@user4581301 所指出的,您不应多次调用srand(time(0)) ,因为它会根据当前系统时间重置随机种子。 If srand(time(0)) is called in rapid succession, the very big number that it will take as a seed (which I believe is the current epoch time ) will be sufficiently close to the previous call that you might not observe significant difference in your RNG.如果srand(time(0))被快速连续调用,它将作为种子的非常大的数字(我相信是当前纪元时间)将足够接近上一次调用,您可能不会观察到显着差异在你的 RNG 中。

Simply moving the srand(time(0));只需移动srand(time(0)); line out of the while loop should do the trick. while 循环中的行应该可以解决问题。

How do I get it so that when the user presses 'y' and the game resets that the random number also changes?我如何得到它,以便当用户按下“y”并且游戏重置随机数也会改变?

You get the next number in the pseudo random sequence by calling rand without calling srand in between.您可以通过调用rand而无需在其间调用srand来获得伪随机序列中的下一个数字。 If you set the random sequence to start from the current timestamp on every iteration, then you get the same number which changes once a second.如果您将随机序列设置为从每次迭代的当前时间戳开始,那么您将获得相同的数字,该数字每秒更改一次。

I also can't use std libraries.我也不能使用标准库。

srand, rand, time, cout and cin are all from the standard library. srand、rand、time、cout 和 cin 都来自标准库。

I think @Mathis has already pointed out the solution.我认为@Mathis已经指出了解决方案。

I am just sharing some insight as to how srand and rand are related.我只是分享一些关于srandrand如何相关的见解。 Consider the below code snippet:考虑下面的代码片段:

#include <iostream>
#include <ctime>
int main()
{
    int i = 0;
    // Uncomment below line to generate new set of random numbers
    // on every execution.
    // srand(time(0));   
    while (i < 5)
    {
        std::cout<< rand() % 10 <<std::endl;
    }
}

Let's say the program generates numbers - 5, 7, 3, 0 and 4 on 1st run.假设程序在第一次运行时生成数字 5、7、3、0 和 4。 If you run the program again, you will see the same set of numbers, ie, 5, 7, 3, 0 and 4. So, although they are random (pseudo random to be precise), but on every program execution, the order of numbers will be same.如果再次运行程序,您将看到相同的一组数字,即 5、7、3、0 和 4。因此,虽然它们是随机的(准确地说是伪随机),但在每次程序执行时,顺序的数字将是相同的。

This is the reason we use srand to specify some seed value.这就是我们使用srand来指定一些种子值的原因。 Seed is any value which is different on each execution.种子是每次执行时不同的任何值。 When we use time(0) as parameter to srand , we make sure that on every program execution, we are providing a new and unique seed.当我们使用time(0)作为srand的参数时,我们确保在每次程序执行时,我们都提供了一个新的唯一种子。 This will make sure that we get truly random set of numbers.这将确保我们得到真正随机的一组数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM