RE: [AD] simple question on randomizing

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


> srand(static_cast<unsigned>(time(NULL)));
>    int random_integer = (rand()% 1000);

srand reinitializes the RNG with a new seed. In your case, with the
current time. Since it is a fast operation, chances are the RNG will
return the time.
The algorithm is probably something like

next = (current * X + Y) & (RAND_MAX-1)

Calling srand each time essentially does:

current = constant
next = (constant * X + Y) & (RAND_MAX-1)

If constant increases steadily (as time usually does), then you end
up with a stream of:

increasing_number * X + Y

which your test exhibits.

So: only call srand once.

-- 
Vincent Penquerc'h 




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/