[12728] in Athena Bugs
Re: decmips 7.7G: rand()
daemon@ATHENA.MIT.EDU (epeisach@MIT.EDU)
Wed Oct 12 20:17:10 1994
From: epeisach@MIT.EDU
Date: Wed, 12 Oct 1994 20:17:03 -0400
To: adamek@MIT.EDU
Cc: bugs@MIT.EDU
In-Reply-To: [12727]
The rand function is known not to produce good random numbers - at least
for the low order bits. If you read the man page for rand, it suggests
that random(3) should be used.
From the rand man page:
The newer random(3) should be used in new applications. The rand sub-
routine remains for compatibility.
If you read the man page for random for the decstations says:
The random/srandom subroutines have (almost) the same calling sequence
and initialization properties as rand/srand. The difference is that
rand(3) produces a much less random sequence - in fact, the low dozen
bits generated by rand go through a cyclic pattern. All the bits gen-
erated by random are usable. For example, "random()&01" will produce a
random binary value.
I think you should replace rand with random, srand with srandom...
(As a footnote, on another machine, the man page even gives the
implementation: I would not be suprised if you could prove that your
sample program must give alternating numbers -> you are only looking at
the top 16 bits of your seed going through an iteration...
static unsigned int next = 1;
int rand( )
{
next = next * 1103515245 + 12345;
return ( (next >>16) & RAND_MAX);
}
void srand (seed)
int seed;
{
next = seed
}
Enjoy....
Ezra