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 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消