C++ C++;在循环/提示时生成新随机数时出现问题
我几乎完成了这个基本的骰子程序,但很难找出我在srand上做错了什么。它应该为用户随机掷2个骰子,用户可以选择保留他们掷的骰子(K)或再次掷骰子(R)。我想程序输出新的“随机”数字骰子辊每次用户输入“R”时,询问他们是否想再次辊,但相同的数字继续显示 程序的其余部分似乎工作得很好——用户与计算机对抗。当用户被要求保留或重新滚动时输入“R”时,如何为roll1和roll2生成新的数字C++ C++;在循环/提示时生成新随机数时出现问题,c++,loops,random,srand,C++,Loops,Random,Srand,我几乎完成了这个基本的骰子程序,但很难找出我在srand上做错了什么。它应该为用户随机掷2个骰子,用户可以选择保留他们掷的骰子(K)或再次掷骰子(R)。我想程序输出新的“随机”数字骰子辊每次用户输入“R”时,询问他们是否想再次辊,但相同的数字继续显示 程序的其余部分似乎工作得很好——用户与计算机对抗。当用户被要求保留或重新滚动时输入“R”时,如何为roll1和roll2生成新的数字 #include <stdlib.h> /// need this for srand() -- fo
#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int main()
{
int iseed = (int)time(0);
srand(iseed);
cout << "Beat the computer! \n";
int roll1 = 1+rand()%6; /// make a random number for die # 1 for user
int roll2 = 1+rand()%6; /// make a random number for die # 2 for user
int UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
char keep;
do
{
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
cout << "\n";
do
{
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep != 'K' && keep != 'R')
{
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
cout << "\n";
}
} while(keep != 'K' && keep != 'R');
if (keep == 'R')
{
cout << "You chose R--let's roll again. \n";
}
else
{
cout << "Great! Your total is " << UserRoll << "\n";
}
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
cout << "\n";
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You won! \n";
}
if (ComputerRoll > UserRoll)
{
cout << "Sorry. You lose. \n";
}
if (ComputerRoll == UserRoll)
{
cout << "It's a tie. \n";
}
return 0;
}
/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 6 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
K
Great! Your total is 10
The computer rolled a 4 and a 6 for a total of: 10
It's a tie.
Process returned 0 (0x0) execution time : 8.763 s
Press any key to continue.
*/
#include///srand()需要此选项--用于随机数
#include///需要这个时间()--时间
#include///需要这张卡吗
使用命名空间std;///cout>
int main()
{
int iseed=(int)时间(0);
srand(iseed);
cout您只滚动了一次,并再次使用滚动的结果。如果用户再次按R,它将再次滚动以更新新的数字。为此,只需添加
void roll(){
roll1 = 1+rand()%6; /// make a random number for die # 1 for user
roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
}
记住:roll1、roll2和UserRoll应该在roll()函数之前全局声明
在main()方法之前,并在用户按R键时调用它,以及
之后
cout太好了!我终于有了一个有效的程序!如果你认为有什么方法可以让这个更整洁、更容易阅读,请告诉我:
#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int roll1,roll2,UserRoll;
char keep;
void roll()
{
roll1 = 1+rand()%6; /// make a random number for die # 1 for user
roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
}
int main()
{
int iseed = (int)time(0);
srand(iseed);
cout << "Beat the computer! \n";
roll();
do
{
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
cout << "\n";
do
{
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep != 'K' && keep != 'R')
{
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
cout << "\n";
}
} while(keep != 'K' && keep != 'R');
if (keep == 'R')
{
cout << "You chose R--let's roll again. \n";
roll();
}
else
{
cout << "Great! Your total is " << UserRoll << "\n";
}
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
cout << "\n";
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You won! \n";
}
if (ComputerRoll > UserRoll)
{
cout << "Sorry. You lose. \n";
}
if (ComputerRoll == UserRoll)
{
cout << "It's a tie. \n";
}
return 0;
}
/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 2 and a 4 for a total of: 6
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 4 for a total of: 8
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 1 for a total of: 5
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 5 and a 5 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 6 and a 1 for a total of: 7
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 5 and a 2 for a total of: 7
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 6 and a 6 for a total of: 12
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
K
Great! Your total is 12
The computer rolled a 3 and a 1 for a total of: 4
Congratulations! You won!
Process returned 0 (0x0) execution time : 19.487 s
Press any key to continue.
*/
#include///srand()需要此选项--用于随机数
#include///需要这个时间()--时间
#include///需要这张卡吗
使用名称空间std;///cout>
int roll1、roll2、UserRoll;
炭保持;
无效卷()
{
roll1=1+rand()%6;///为用户的die#1创建一个随机数
roll2=1+rand()%6;///为用户的die#2创建一个随机数
UserRoll=roll1+roll2;///合计用户的骰子1和骰子2之和
}
int main()
{
int iseed=(int)时间(0);
srand(iseed);
您是否尝试通过在循环中调用srand
来重新为其设定种子?@macroland,这有什么意义?srand()
应该只调用一次。@SidS,以保证获得一个新的随机数序列。@macroland——正如Sid S所说,srand()
只应调用一次。已对代码进行编辑以使其更清晰。请签出并接受编辑,您可以在返回0;之前将if语句更改为multiple if-else if,从而获得编辑。您可以提高代码的时间效率,因为当任何条件变为真时,它不会检查其余条件。yo你可以读更多关于它的内容
#include <stdlib.h> /// need this for srand() -- for random numbers
#include <time.h> /// need this for time() -- time
#include <iostream> /// need this for cout<< and cin>>
using namespace std; /// need this for cout<< and cin>>
int roll1,roll2,UserRoll;
char keep;
void roll()
{
roll1 = 1+rand()%6; /// make a random number for die # 1 for user
roll2 = 1+rand()%6; /// make a random number for die # 2 for user
UserRoll = roll1 + roll2; /// totals the sum of die 1 and die 2 for the user
}
int main()
{
int iseed = (int)time(0);
srand(iseed);
cout << "Beat the computer! \n";
roll();
do
{
cout << "You rolled a " << roll1 << " and a " << roll2 << " for a total of: " << UserRoll << "\n";
cout << "\n";
do
{
cout << "Would you like to keep this total, or roll again? \n";
cout << "\n";
cout << "Enter \"K\" for keep and \"R\" for roll again: \n";
cin >> keep;
if (keep != 'K' && keep != 'R')
{
cout << "That is not a valid choice. Please choose Y to keep your total or N to roll again. " << endl;
cout << "\n";
}
} while(keep != 'K' && keep != 'R');
if (keep == 'R')
{
cout << "You chose R--let's roll again. \n";
roll();
}
else
{
cout << "Great! Your total is " << UserRoll << "\n";
}
} while (keep == 'R');
int roll3 = 1+rand()%6; /// make a random number for die # 1 for computer
int roll4 = 1+rand()%6; /// make a random number for die # 2 for computer
int ComputerRoll = roll3 + roll4; /// totals the sum of die 1 and die 2 for the computer
cout << "The computer rolled a " << roll3 << " and a " << roll4 << " for a total of: " << ComputerRoll << "\n";
cout << "\n";
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You won! \n";
}
if (ComputerRoll > UserRoll)
{
cout << "Sorry. You lose. \n";
}
if (ComputerRoll == UserRoll)
{
cout << "It's a tie. \n";
}
return 0;
}
/*
SAMPLE RUNS:
------------
Beat the computer!
You rolled a 2 and a 4 for a total of: 6
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 4 for a total of: 8
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 4 and a 1 for a total of: 5
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 5 and a 5 for a total of: 10
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 6 and a 1 for a total of: 7
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 5 and a 2 for a total of: 7
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
R
You chose R--let's roll again.
You rolled a 6 and a 6 for a total of: 12
Would you like to keep this total, or roll again?
Enter "K" for keep and "R" for roll again:
K
Great! Your total is 12
The computer rolled a 3 and a 1 for a total of: 4
Congratulations! You won!
Process returned 0 (0x0) execution time : 19.487 s
Press any key to continue.
*/