C++ 如何在存储的两个变量之间生成随机数?
可能重复:C++ 如何在存储的两个变量之间生成随机数?,c++,C++,可能重复: 我试图创建一个程序,让计算机猜测用户头脑中的数字。唯一需要用户输入的是猜测是否过高、过低或正确。我遇到了一个问题,在两个变量之间生成一个随机数,根据之前的猜测存储最小值和最大值。这是我的密码: #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { sran
我试图创建一个程序,让计算机猜测用户头脑中的数字。唯一需要用户输入的是猜测是否过高、过低或正确。我遇到了一个问题,在两个变量之间生成一个随机数,根据之前的猜测存储最小值和最大值。这是我的密码:
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
静态施法(时间(0));
int compGuess=rand()%100+1;//生成1-100之间的数字
int highestNumber=100;
int lowerstnumber=1;
半焦就绪;
成功;
成功;
int=0;
准备好了;
如果(就绪=='y')
{
做
{
cout要生成最小值和最大值之间的随机数,请使用:
int randNum = rand()%(max-min + 1) + min;
(包括最大值和最小值)非常快,非常简单:
srand(time(NULL)); // Seed the time
int finalNum = rand()%(max-min+1)+min; // Generate the number, assign to variable.
就是这样。然而,这是偏向低端的,但是如果您使用,您可以使用random
标题来避免这种偏向,如下所示:
#include <random>
std::mt19937 rng(seed);
std::uniform_int_distribution<int> gen(min, max); // uniform, unbiased
int r = gen(rng);
这是从中得到的,如果你有一个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);
//确保包含随机数生成器等
#包括
//将为发电机播种的随机设备
std::随机_装置播种机;
//然后做一个梅森捻线机引擎
std::mt19937发动机(播种机());
//然后是简单的部分…分布
标准:均匀分布距离(最小值、最大值);
//然后生成如下所示的整数:
int compGuess=距离(发动机);
这可能稍微容易掌握,因为你不需要做任何涉及模和垃圾的事情……虽然它需要更多的代码,但是知道一些新C++的东西总是很不错……
希望这有帮助
-卢克将100
替换为highestNumber
,将1
替换为lowerstnumber
?除此之外,看起来你还是在正确的轨道上。你的线路上有一个操作顺序问题,标记为“无法产生期望的结果”。正确地插入括号,你会很好。这里有许多示例答案。@CarlNorum:很抱歉重复。=(这个答案对于大多数用途来说可能已经足够好了,但是OP应该注意,如果rand()的范围
不能被max-min+1
整除。你能告诉我更多关于它的信息吗?rand()的范围是什么平均值?rand
返回介于0
和rand\u MAX
之间的数字。这是rand\u MAX+1
可能的模输入值。有MAX-min+1
可能的输出值。如果输入与输出的比率不是整数,则您最终会对较低的数字产生一点偏差。Imag写出所有RAND_MAX+1
输入:0,1,2,…RAND_MAX
,对吗?现在在它们下面写出post modulo(对于min=0
和MAX=5
)例如:0,1,2,3,4,5,0,1,2,4,5,0,1,2,3,4,5
,等等。现在如果RAND_MAX
不能被6
整除,那么在最后会得到一个部分循环0,1,2
…或者类似的东西。这意味着0,1,2
的输出比3,4,5
的输出更多-给你的算法一个偏差关于0,1,2
答案。啊!我明白你的意思。谢谢。这个答案对于大多数用途来说可能已经足够好了,但是OP应该意识到,如果rand()的范围
不能被最高位数字-最低位数字+1整除
@dmakaitis:谢谢,现在它可以工作了。这个答案对于大多数应用来说可能已经足够好了,但是OP应该意识到,如果rand()的范围<代码>不能被<代码> max -min + 1 < /代码>整除。@ CarlNorum,我更新了我的答案来反映它。看起来不错。+ 1。对你来说,CalLNORM也显示了如何消除正常C++中的偏倚。D@Link:我将保留删除偏差的示例以备将来使用,因为偏差现在不是一个大问题。非常感谢!遗憾的是,我没有使用C++11编译器呃,对不起,我想我应该在OP中提到我的编译器
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;
}
//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);