小编Jam*_*are的帖子

Rand()在C中并不是唯一的

我正在使用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)

谢谢!

c random

1
推荐指数
1
解决办法
117
查看次数

标签 统计

c ×1

random ×1