well well, I feel kinda n00b here but something is puzzling me.
If I do
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main (void)
{
srand (time (NULL)); // initialize random seed
// generate a pseudo-random number ranging from 0 to 99
printf ("%d\n", (int) (100 * (float) rand () / (RAND_MAX + 1)));
}
and I run my application, I get the same number each time I run it.
Now if I do
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main (void)
{
srand (time (NULL)); // initialize random seed
for (i = 0; i < 100; i++)
rand (); // call rand a couple times
// generate a pseudo-random number ranging from 0 to 99
printf ("%d\n", (int) (100 * (float) rand () / (RAND_MAX + 1)));
}
Now I get a different number each time I run it (which is what I expected initially).
But could someone tell me WHY on Earth I need to call rand() a great number of times before using it really to have a random number ? Or put it differently why is it ALWAYS the same numbers that pop up during the first rand() calls, and this NO MATTER what I put in the seed ?
Any explanation ???
