int a=0;srand((unsigned)time(0));a=rand()%10;能出现a==0 吗?a== 10 能出现吗?
2 回答

富国沪深
TA贡献1790条经验 获得超9个赞
#include <stdlib.h>
#include <time.h>
int a = 0;
srand(time(NULL));
a = rand()%10;
a可能的随机范围0-9,不会出现10.

有只小跳蛙
TA贡献1824条经验 获得超8个赞
不能出现10,但是0是会出现的。想出现10的话改成
#include <iostream>
#include <cstdlib>//cstdlib文件包含处理生成随机数的函数以及其他函数
#include <ctime>//与随机函数同时使用
using namespace std;
int main()
{
srand(time(0));//根据当前时间生成随机数生成器种子
int randomNumber = rand();//生成随机数,rand() 生成的是0~32767之间的一个随机数。
int die = (randomNumber % 11) ;//获得0--10的随机数
cout<<"You rolled a :"<<die<<endl;
cin.get();
cin.get();
return 0;
}
- 2 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消