我正在尝试创建一个程序,其中计算机猜测用户在他/她心中的数字。唯一需要的用户输入是猜测是否过高,过低或正确。我在根据先前的猜测存储最小值和最大值的两个变量之间生成随机数时遇到问题。这是我的代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast <unsigned int> (time(0)));
int compGuess = rand() % 100 +1; //Generates number between 1 - 100
int highestNumber = 100;
int lowestNumber = 1;
char ready;
char highLowSuccess;
bool success;
int tries = 0;
cout << "Please pick a number between 1 - 100. I will guess your number. Don't tell me what it is!\n\n";
do
{
cout << "Are you ready? (y/n)\n\n";
cin >> ready;
if (ready == 'y')
{
do
{
cout << "Is your number " << compGuess << "?\n\n";
cout << "High, Low or Success?";
++tries;
cin >> highLowSuccess; //User input telling the computer whether its too high, too low, or a success
if (highLowSuccess == 'h') //Executes code if number guessed was too high.
{
highestNumber = compGuess - 1; //Stores variable indicating the highest possible number based on user input
compGuess = rand() % highestNumber +1; //Generates a new random number between 1 and the new highest possible number
success = false;
}
else if (highLowSuccess == 'l') //Executes code if number guessed was too low.
{
lowestNumber = compGuess + 1;//Stores variable indicating the lowest possible number based on user input
compGuess = (rand() % highestNumber - lowestNumber + 1) + lowestNumber // <---- Not producing the desired result
success = false;
}
else if (highLowSuccess == 's') //Executes code if the computer's guess was correct.
{
cout << "I guessed your number! It only took me " << tries << " tries!";
success = true;
}
} while (success != true);
}
else
{
continue;
}
} while (ready != 'y');
return 0;
}
highestNumber是max应该是什么,而minimumNumber是min应该是什么。我需要一个方程式,让我生成一个随机数,同时考虑最高和最低的数字。
请原谅我,如果答案非常简单,我就是一个菜鸟程序员。的xD
答案 0 :(得分:42)
要在最小值和最大值之间生成随机数,请使用:
int randNum = rand()%(max-min + 1) + min;
(包括最大和最小)
答案 1 :(得分:33)
真的很快,很容易:
srand(time(NULL)); // Seed the time
int finalNum = rand()%(max-min+1)+min; // Generate the number, assign to variable.
就是这样。但是,这偏向于低端,但如果您使用的是C++ TR1/C++11,则可以使用random
标头来避免这种偏差:
#include <random>
std::mt19937 rng(seed);
std::uniform_int_distribution<int> gen(min, max); // uniform, unbiased
int r = gen(rng);
但你也可以删除正常C ++中的偏见:
int rangeRandomAlg2 (int min, int max){
int n = max - min + 1;
int remainder = RAND_MAX % n;
int x;
do{
x = rand();
}while (x >= RAND_MAX - remainder);
return min + x % n;
}
这是从this post.
获得的答案 2 :(得分:17)
如果你有一个C ++ 11编译器,你可以使用c ++的伪随机数功能为未来做好准备:
//make sure to include the random number generators and such
#include <random>
//the random device that will seed the generator
std::random_device seeder;
//then make a mersenne twister engine
std::mt19937 engine(seeder());
//then the easy part... the distribution
std::uniform_int_distribution<int> dist(min, max);
//then just generate the integer like this:
int compGuess = dist(engine);
这可能稍微容易掌握,因为你不必做任何涉及模数和废话的事情......虽然它需要更多的代码,但是知道一些新的C ++东西总是很好的..
希望这会有所帮助 - 卢克
答案 3 :(得分:5)
rand() % ((highestNumber - lowestNumber) + 1) + lowestNumber