[問題] 簡單程式,但執行速度不快
開發平台(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):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.25.244
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1407930666.A.A83.html
→
08/13 19:53, , 1F
08/13 19:53, 1F
一秒見效,所以是每次取seed都會花很多時間計算囉?? 感謝
※ 編輯: FirstAnd15th (114.34.25.244), 08/13/2014 20:07:13
推
08/13 20:21, , 2F
08/13 20:21, 2F
推
08/13 20:45, , 3F
08/13 20:45, 3F
感謝,我會去弄清楚
※ 編輯: FirstAnd15th (114.34.25.244), 08/13/2014 22:30:00
推
08/13 22:48, , 4F
08/13 22:48, 4F
→
08/13 22:49, , 5F
08/13 22:49, 5F
→
08/13 22:49, , 6F
08/13 22:49, 6F
我懂了!! 所以不是速度慢,是秒針就是跑這個速度 XDD
推
08/13 23:00, , 7F
08/13 23:00, 7F
我是自學C,不能怪老師,只能怪自己沒用功嚕
※ 編輯: FirstAnd15th (114.34.25.244), 08/14/2014 11:15:46
推
08/14 21:58, , 8F
08/14 21:58, 8F
→
08/15 00:50, , 9F
08/15 00:50, 9F