vendredi 2 février 2018

C How to Keep a Random Variable From Repeating the Same Number

So I'm just learning C and I would like to know how you could prevent a variable randomized with the rand() function from repeating the same number. I have a script which simply randomizes and prints a variable in a for loop 4 times. How could I make it so the variable never gets the same number after each time it uses the rand() function?

#include <stdio.h>
#include <stdlib.h>
int randomInt;
int main()
{
    srand(time(0));
    for (int i = 0; i < 4; ++i) {
        randomInt = rand() % 4;
        printf("%d\n", randomInt);
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire