lundi 6 avril 2020

Random number stays the same

So I' ve written this piece of code inside a function, that its purpose is to generate a number in the [1-3] range so I can use it in a swich/case statement. The first time the program uses the function I get a random number in [1-3]. When the program uses the function again, it only gives the same number as the first.

srand((int)time(NULL));
    double a;
    int num;

    a = (double)rand() / ((double)RAND_MAX + 1);
    a = a * 3 + 1;
    num = (int)a;



Aucun commentaire:

Enregistrer un commentaire