I got a very strange problem with rand(). Let's say I want a different sequence of random number for each run, I write;
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
int a, b;
a = rand();
b = rand();
printf("a = %d\nb = %d", a, b);
return 0;
}
For "b", everything works fine, it is truly random (or anyway: as random as this function can get). "a" behave very differently, in fact I tried this many time, and the first pseudoradom number I gererate is not random at all. In this example, I got a = 23619 the first time, then 23624, 23659, ... The more I wait, the higher it gets, as if the first random number generated was not random at all, but followed seconds.
The best solution I found is;
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
rand();
int a, b;
a = rand();
b = rand();
printf("a = %d\nb = %d", a, b);
return 0;
}
To get rid of the first "random" number. Is there a better method ?