-1

我正在编写一个游戏,其中计算机选择用户必须猜测的随机数。然后,由用户选择计算机必须猜测的数字(他只是猜一个数字)。问题是我在游戏第二部分使用的 rand() 函数有时会提供一个超出新范围的数字。例如,如果新范围是低 = 4 和高 = 10,rand() 提供 12,这是不正确的。我试图理解为什么会出现这个错误,但我找不到它。我正在用 Dev-C++ 编写。代码就在下面,然后是输出。感谢您的帮助和时间。

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>




using namespace std;

int main()
{
  int computer_guess, nbr, nbrguesses, count_user_guess=0, count_computer_guess=0; 
  char user_comparison;

  int  low=1, high=99;  //initialize low to the lower boundary and high to the higher     boundary

  srand(time(0));
  nbr=rand()% 99 + 1;  //computer chooses a random number between 1 and 99...

  do
  {
    cout << "guess a number now! " << endl;
    cin >> nbrguesses;     //user enters the number he guessed...
    if ( nbrguesses>nbr)
       cout << "too big" <<endl;
    else if ( nbrguesses<nbr)
       cout << "too small" << endl;
    else if ( nbrguesses==nbr)
       cout << " you found the correct number! " <<endl;

    count_user_guess++;  //count the number of guesses from the user
  }
  while (nbrguesses!=nbr);

  cout << "you tried " << count_user_guess <<" times to find the right number " <<      endl<<endl;

  cout << "----------------------------------------------------------------------" << endl;
  cout << "Now, the computer will guess your number " << endl;

  do
  {
    srand(time(0));
    computer_guess=rand()% high + low;    //computer guesses the number
    cout << "computer guess: " << computer_guess <<endl;
    cout << "is it the correct number?" << endl;
    cin >> user_comparison;     //if user enter 
    if (user_comparison=='>')  // character '>' it means the number guessed by computer is too big
    {
       cout << "too big" <<endl;
       high= computer_guess-1;   //high is given a new boundary 
       cout << "***Current value of high = " << high << " and low = " << low << endl;    //display current boundaries

    }
    else if ( user_comparison=='<')  //the number guessed by computer is too small
    { 
      cout << "too small" << endl;
      low= computer_guess+1;  //low is given a new boundary
      cout << "***Current value of low = " << low << " and high = " << high << endl;  //display current boundaries

    }
    else if ( user_comparison=='=')
       cout << "Computer found the correct number! " <<endl;

    count_computer_guess++;  //count number of guesses from computer
  }
  while (user_comparison!='=');

  cout << "The computer tried " << count_computer_guess <<" times to find the right number " << endl;

  cout << "The Game is over now, have a good day!" << endl;
  getch();

  return 0; 

}

//**************Output******************************************

guess a number now!
50
too big
guess a number now!
25
you found the correct number!
you tried 2 times to find the right number

----------------------------------------------------------------------
Now, the computer will guess your number
computer guess: 11
is it the correct number?
>
too big
***Current value of high = 10 and low = 1
computer guess: 3
is it the correct number?
<
too small
***Current value of low = 4 and high = 10
computer guess: 12
is it the correct number?
4

3 回答 3

4

你必须更换

computer_guess=rand()% high + low;

经过

computer_guess=rand()%(high- low + 1) + low;

在您的示例中,您有低 = 4 和高 = 10。但是您想要一个介于 0 和 6 之间的随机数并添加 4。不是介于 0 和 10 之间的一个,从那时起您可以获得高达 14 的结果。如果您不想包括上限(间隔 [low,high[ 而不是 [low,high] ),您必须省略括号中的 +1。

为什么

rand() 返回一个相当大的整数。我们想从中得到一个区间 [a,b] 中的随机数。如果我们取 rand()%5,我们会得到一个 0,1,2,3 或 4 的数字,所以从区间 [0,4] 开始。通常 rand()%C 给出区间 [0,C-1] 内的随机数。

如果我们添加一个常数,例如 Rand()%C + D,则间隔移动:[D,C-1 +D]。回到问题,我们希望区间为 [a,b]。因此,我们希望下键 a=D,上键 C-1+D = b。这我们可以转换为 C = b-D+1 = b-a+1。所以我们要使用 rand()%(b-a+1) + a。

希望这能解释一下事情是如何运作的。

于 2012-04-07T01:54:29.353 回答
1

你很接近,但试试这个:

rand()%(high-low)+low;

于 2012-04-07T01:53:57.270 回答
1
srand(time(0));
computer_guess=rand()% high + low;    //computer guesses the number

首先,不要srand在循环内调用。只需在程序启动时调用一次。

其次,这个逻辑是错误的。它应该是:

computer_guess=low + (rand() % (high-low+1));

(这假设 high 和 low 包含在内。也就是说,如果 high 是 10,low 是 1,那么 1 和 10 都是可以接受的猜测。)

于 2012-04-07T01:54:27.633 回答