C++ 防止不断重新分配随机整数
我正在做一个小游戏,随机选择一个数字,然后让用户猜,根据用户的选择给出指示(大,小…)。要生成0到100之间的随机数,我使用以下代码:C++ 防止不断重新分配随机整数,c++,C++,我正在做一个小游戏,随机选择一个数字,然后让用户猜,根据用户的选择给出指示(大,小…)。要生成0到100之间的随机数,我使用以下代码: int random = rand() % 101; 这很好,但唯一的问题是该变量不断被重新分配不同的值。我怎样才能防止这种情况 #include <iostream> #include <string> #include <cmath> using namespace std; int tries;//number of
int random = rand() % 101;
这很好,但唯一的问题是该变量不断被重新分配不同的值。我怎样才能防止这种情况
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int tries;//number of tries allowed
bool win;//whether the user has won the game or not
int main(){
string gameType;
cout << "Please enter your game type; \"easy\", \"medium\", \"hard\", and \"expert\"." << endl;
cin >> gameType;
if(gameType == "easy"){
tries = 40;
cout << "You have 40 tries." << endl;
}else if (gameType == "medium"){
tries = 20;
cout << "You have 20 tries." << endl;
}else if (gameType == "hard"){
tries = 10;
cout << "You have 10 tries." << endl;
}else if (gameType == "expert"){
tries = 5;
cout << "You have 5 tries.";
}
cout << "Please enter a number between 0 and 100." << endl;
while (win == false){
int random = rand() % 101;//random number generation between 0 and 100
return(random);
bool valid = false;//if the user's number is valid (0 >= ; <= 100)
int usedTries = 0;//the tries that the user has used up
int selection;
cin >> selection;
if (selection > 100){
cout << "Your number cannot be above 100." << endl;
valid = false;
}else if (selection < 0){
cout << "Your number cannot be below 0";
valid = false;
}else{
valid = true;
}
if (valid == true){
if (selection > random){//bigger than target number
cout << "Smaller!" << endl;
usedTries = usedTries + 1;
}else if (selection < random){//smaller than target number
cout << "Bigger!" << endl;
usedTries = usedTries + 1;
}else if (selection == random){//the user has guessed the right answer
cout << "Yay! You win!" << endl;
win = true;
}
if (usedTries >= tries){//user has used up his number of tries
cout << "Sorry, you've lost the game. Try again later." << endl;
win = false;//to end the loop and terminate the game
}
}
}
return(0);
}
#包括
#包括
#包括
使用名称空间std;
智力测验//允许的尝试次数
布尔温//用户是否赢得游戏
int main(){
字符串游戏类型;
cout配子型;
如果(游戏类型==“轻松”){
尝试=40;
cout只有在您执行赋值时才会为其赋值。根据您的描述,它是在循环内赋值的。您可能希望将赋值移出循环
典型的结构如下所示:
呼叫srand
生成随机数
从用户那里得到猜测
如果猜错了,转到3
如果用户想再次访问,请转到2
仅当您执行赋值时才会为其赋值。根据您的描述,它是在循环内赋值的。您可能希望将赋值移出循环
典型的结构如下所示:
呼叫srand
生成随机数
从用户那里得到猜测
如果猜错了,转到3
如果用户想再次访问,请转到2
如果您具有相同的结果编号并需要真实的radom值,则应在调用rand
之前至少使用srand
函数一次。传递给srand
一些随机值,简单的方法是以秒为单位传递当前时间等。如果您具有相同的结果编号并需要真实的radom值,则应在调用rand
之前至少使用srand
函数一次。传递给srand
一些随机值,简单的方法是以秒为单位传递当前时间等。正如其他人指出的,您应该只分配一次。无论哪种方式,您都可以将其更改为:
static int random = rand() % 101;
这将只分配一次。正如其他人所指出的,您应该只分配一次。无论哪种方式,您都可以将其更改为:
static int random = rand() % 101;
这将只分配一次。您能否详细说明“不断被重新分配不同的值”的含义现在,我不知道你在问什么。把作业从主播放循环中去掉?…不要分配给它不止一次?你可能应该给我们看更多的代码,因为这个问题没有意义。仍然有人认为机器可以计算随机数或以随机方式…你需要用一个deb逐步完成你的代码乌格。观察random
的值,你就会知道它什么时候会改变。然后想想它为什么会改变并修复它。如果你觉得很难,就假装你在一步一步地告诉别人代码在做什么。你能详细解释一下“不断地被重新分配不同的值”是什么意思吗现在,我不知道你在问什么。把作业从主播放循环中去掉?…不要分配给它不止一次?你可能应该给我们看更多的代码,因为这个问题没有意义。仍然有人认为机器可以计算随机数或以随机方式…你需要用一个deb逐步完成你的代码ugger.观察random
的值,你会看到它何时改变。然后想想它为什么改变并修复它。如果你觉得很难,就假装你正在告诉别人代码在做什么。最好不要重新分配变量。分配一次值,然后将该值保存在某个地方。这样会更好最好不要重新分配变量。分配一次值,然后将该值保存在某个地方。