Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

srand(time(NULL)) doesn't change seed value quick enough [duplicate]

Tags:

I have written a simple random number generator in C. int l is the lower bound and int u is the upper bound.

It works just fine, however I have a question regarding seeding it. If I was to run this in a loop, time(NULL) doesn't change the seed value quick enough to be able to prevent getting a consecutive series of random numbers that are exactly the same.

I'm wondering how anybody else might have approached this problem. All the examples I've found online use time(NULL) as the seed value generator.

int generateRandom(int l, int u) {    srand(time(NULL));     int r = rand() % ((u - l) + 1);    r = l + r;     return r; } 

If I was to run these lines of code right next to each other, both Rand1 and Rand2 would be exactly the same.

printf("Rand1 = %d\n", generateRandom(10, 46)); printf("Rand2 = %d\n", generateRandom(10, 46)); 
like image 767
Chris Avatar asked Apr 07 '11 01:04

Chris


People also ask

How does Srand time NULL )) work?

Any other value for the seed produces a different sequence. srand(time(NULL)); makes use of the computer's internal clock to control the choice of the seed. Since time is continually changing, the seed is forever changing.

What is the purpose of using function Srand ()? To set the seed of rand () function?

srand() uses its argument seed as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is not called, the rand() seed is set as if srand(1) was called at program start. Any other value for seed sets the generator to a different starting point.

What happens if the same seed value is always used for generating random numbers?

A pseudorandom number generator's number sequence is completely determined by the seed: thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers. The choice of a good random seed is crucial in the field of computer security.

What does Srand time do?

srand is a random number generator function which will randomize the number produced by rand function.


1 Answers

srand(time(NULL)) should be run exactly once to intialise the PRNG. Do this in Main when the application starts.

Explanation:

A PRNG (Pseudo-Random Number Generator) generates a deterministic sequence of numbers dependent on the algorithm used. A given algorithm will always produce the same sequence from a given starting point (seed). If you don't explicitly seed the PRNG then it will usually start from the same default seed every time an application is run, resulting in the same sequence of numbers being used.

To fix this you need to seed the PRNG yourself with a different seed (to give a different sequence) each time the application is run. The usual approach is to use time(NULL) which sets the seed based on the current time. As long as you don't start two instances of the application within a second of each other, you'll be guaranteed a different random sequence.

There's no need to seed the sequence each time you want a new random number. And I'm not sure about this, but I have the feeling that depending on the PRNG algorithm being used re-seeding for every new number may actually result in lower randomness in the resulting sequence.

like image 93
Andrew Cooper Avatar answered Sep 23 '22 04:09

Andrew Cooper