我正在使用rand()函数生成随机巧克力.即使我播种了时间,rand()总是给我相同的数字,我不知道如何解决它?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
int main() {
/* Seed the random number generator */
srand48(time(0));
double t=rand()%5;
printf("Your selected chocolate will be: \n");
if(t==0){
printf("Caramel\n");
}
else if(t==1){
printf("Milk\n");
}
else if(t==2){
printf("Sweet\n");
}
else if(t==3){
printf("Semi-sweet\n");
}
else {
printf("Dark\n");
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!