C Yahtzee 模拟器在 Windows 中进行了大量的滚动,而不是在 Linux 中
C Yahtzee simulator takes insane amount of rolls in Windows, not in Linux
我想输入一些 "easy" 乱用 rand 和 srand 函数的代码,我尝试编写 Yahtzee rolls 的模拟器。它随机掷出 5 个骰子,如果它们匹配,它会打印出你有一个 Yahtzee 以及需要重新掷多少次才能得到它。我在 Windows 上的 Ubuntu 虚拟机中输入了这个。工作正常并获得合理的重投结果(1 到 4000 之间)。但是,当我对 Windows 使用相同的代码时,总是需要 5000 万次重新滚动才能获得 Yahtzee。为什么会这样?这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int dice1, dice2, dice3, dice4, dice5, count=0, e=1;
while(e==1){
srand(time(NULL)+rand());
dice1 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice2 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice3 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice4 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice5 = rand () % (6) + 1;
if(dice1 == dice2 && dice2 == dice3 && dice3 == dice4 && dice4 == dice5){
printf("\tYAHTZEE! of %i's\n\tIt took %i rolls\n", dice1, count);
if(count >= 2920) printf("+++LESS THAN A 10%% CHANCE!+++\n");
count = 0;
scanf("%i", &e);
} else count++;
}
return 0;
}
我试过只使用第一个 srand,但它一直在发生。
一秒钟,time(NULL)
将 return 相同的数字。相同的种子将产生相同系列的随机数。作为第一个语句之一调用 srand(time(NULL))
一次。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int dice1, dice2, dice3, dice4, dice5, count=0, e=1;
srand(time(NULL));//call here outside loop so it isn't called again too soon
while(e==1){
dice1 = rand () % (6) + 1;
dice2 = rand () % (6) + 1;
dice3 = rand () % (6) + 1;
dice4 = rand () % (6) + 1;
dice5 = rand () % (6) + 1;
if(dice1 == dice2 && dice2 == dice3 && dice3 == dice4 && dice4 == dice5){
printf("\tYAHTZEE! of %i's\n\tIt took %i rolls\n", dice1, count);
if(count >= 2920) printf("+++LESS THAN A 10%% CHANCE!+++\n");
count = 0;
scanf("%i", &e);
} else count++;
}
return 0;
}
我想输入一些 "easy" 乱用 rand 和 srand 函数的代码,我尝试编写 Yahtzee rolls 的模拟器。它随机掷出 5 个骰子,如果它们匹配,它会打印出你有一个 Yahtzee 以及需要重新掷多少次才能得到它。我在 Windows 上的 Ubuntu 虚拟机中输入了这个。工作正常并获得合理的重投结果(1 到 4000 之间)。但是,当我对 Windows 使用相同的代码时,总是需要 5000 万次重新滚动才能获得 Yahtzee。为什么会这样?这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int dice1, dice2, dice3, dice4, dice5, count=0, e=1;
while(e==1){
srand(time(NULL)+rand());
dice1 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice2 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice3 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice4 = rand () % (6) + 1;
srand(time(NULL)+rand());
dice5 = rand () % (6) + 1;
if(dice1 == dice2 && dice2 == dice3 && dice3 == dice4 && dice4 == dice5){
printf("\tYAHTZEE! of %i's\n\tIt took %i rolls\n", dice1, count);
if(count >= 2920) printf("+++LESS THAN A 10%% CHANCE!+++\n");
count = 0;
scanf("%i", &e);
} else count++;
}
return 0;
}
我试过只使用第一个 srand,但它一直在发生。
一秒钟,time(NULL)
将 return 相同的数字。相同的种子将产生相同系列的随机数。作为第一个语句之一调用 srand(time(NULL))
一次。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int dice1, dice2, dice3, dice4, dice5, count=0, e=1;
srand(time(NULL));//call here outside loop so it isn't called again too soon
while(e==1){
dice1 = rand () % (6) + 1;
dice2 = rand () % (6) + 1;
dice3 = rand () % (6) + 1;
dice4 = rand () % (6) + 1;
dice5 = rand () % (6) + 1;
if(dice1 == dice2 && dice2 == dice3 && dice3 == dice4 && dice4 == dice5){
printf("\tYAHTZEE! of %i's\n\tIt took %i rolls\n", dice1, count);
if(count >= 2920) printf("+++LESS THAN A 10%% CHANCE!+++\n");
count = 0;
scanf("%i", &e);
} else count++;
}
return 0;
}