mardi 13 novembre 2018

Rand() not unique in C

I am using a rand() function to generate a random chocolate. Even though I have seeded the time, rand() always gives me the same number and I'm not sure how to fix it?

#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;
}

Thanks!




Aucun commentaire:

Enregistrer un commentaire