開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC in Linux
問題(Question):
利用亂數跑模擬骰子的程式,簡單來說就是擲骰子,每按ENTER就出現一個點數值
但我連按ENTER,資料會卡卡的,像是計算速度不夠快
預期的正確結果(Expected Output):
dice number is : 2
dice number is : 1
dice number is : 4
dice number is : 3
dice number is : 5
dice number is : 1
dice number is : 3
希望跑出來就是這樣的結果
錯誤結果(Wrong Output):
dice number is : 3
dice number is : 3
dice number is : 2
dice number is : 2
dice number is : 2
dice number is : 2
dice number is : 2
dice number is : 1
dice number is : 1
dice number is : 1
dice number is : 1
dice number is : 1
dice number is : 1
會出現連續一樣的結果好幾次(秒)後才會有新的結果
程式碼(Code):(請善用置底文網頁, 記得排版)
int result;
double r01, r17;
while (getchar() != EOF)
{
//random seed
srand( (unsigned) time (NULL) );
//[0, 1)
r01 = (double) ( rand() / (RAND_MAX + 1.0) );
//[1, 7) , like a dice
r17 = r01 * (7.0 - 1.0) + 1.0;
//output
result = (int) (r17);
printf("dice number is : %d", result);
}
補充說明(Supplement):