rand for statistical purposes

I read that rand should not be used for serious statistical purposes. Does anyone have an explanation for this point of view? What are more suitable alternatives?
It's not very random.

If you want better, look at Boost's random number library, or perhaps randomlib (nice interface for a Mersenne twister). There are many more; google will present lots.
Thanks - I'll have a look.

Additional question: Do you know the algorithm that rand uses? Is it implementation specific?
It is implementation specific. The standard does not specify anything beyond "The rand function returns a pseudo-random integer."

It gives this as a possible implementation, so you can see the potentially shoddy quality of random numbers you might get out of calling rand(). :)

1
2
3
4
5
6
7
8
9
10
11
12
static unsigned long int next = 1;
int rand(void)
// RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}
Last edited on
Last edited on
Topic archived. No new replies allowed.