我正在使用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;
}
谢谢!
答案 0 :(得分:5)