so I am trying to fill an array with random integers.
for (int i = 0; i < 2; i++){
srand(time(NULL));
array[i]=rand()%30; }`
Here is my code so far. I am currently getting the same random number twice. Would like to know if there is a way around this?
rand()
usually uses a PRNG, which uses the seed to generate the random number. srand ( time(NULL) );
is used to seed the pseudo-random number generator.
Currently, the time granularity of time()
is 1 second, so if you seed the PNRG with the same value [time(NULL);
] multiple time within the granularity period, call will to rand()
generate the same random number.
Move the srand(time(NULL)
outside the loop.