开发平台(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):