初级C++里rand的用法-三个随机筛子游戏

这是一道很简单的使用rand的问题,使用C++程序滚动3个骰子,显示这些骰子的值,并显示三颗骰子加起来的总和。下面是我写的,本来应该要随机出现三个值的,但是我运行了之后,... 这是一道很简单的使用rand的问题,使用C++程序滚动3个骰子,显示这些骰子的值,并显示三颗骰子加起来的总和。 下面是我写的,本来应该要随机出现三个值的,但是我运行了之后,一直都是显示一样的值,有人能帮我找到问题出在哪里吗?(PS:因为我还是新手,写的东西很简单,拜托了)

#include <iostream>
using namespace std;
int main()
{
int d1;
int d2;
int d3;
int total;

d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
d3 = rand() % 6 + 1;
cout << "The 1st Dice is " << d1 << endl;
cout << "The 2nd Dice is " << d2 << endl;
cout << "The 3rd Dice is " << d3 << endl;
total = d1+d2+d3;
cout << "The Total value of these dices is " << total << endl;

return 0;
}
展开
 我来答
8号闪电人
2011-02-27 · TA获得超过324个赞
知道小有建树答主
回答量:152
采纳率:0%
帮助的人:191万
展开全部
你的随机数是编译生成的,需要使用随机数种子才能生成真的随机数。

srand(time(0));

需要头文件
#include <ctime>

#include <iostream>
using namespace std;
int main()
{
int d1;
int d2;
int d3;
int total;

srand(time(0));
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
d3 = rand() % 6 + 1;
cout << "The 1st Dice is " << d1 << endl;
cout << "The 2nd Dice is " << d2 << endl;
cout << "The 3rd Dice is " << d3 << endl;
total = d1+d2+d3;
cout << "The Total value of these dices is " << total << endl;

return 0;
}
百度网友96aa8d3
2011-02-27 · TA获得超过433个赞
知道小有建树答主
回答量:2306
采纳率:75%
帮助的人:1040万
展开全部
cin>>seed;
srand(seed);
for (int i=0;i<10;i++)
{
int temp=rand()%10+1; // 1-100
h.push_back(temp);
}

你先看看怎么产生随机数
追问
你好,我补充了问题细节,麻烦你再帮我看一次可以么?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式