query on random(3) algorithm and usage

Bob Lewis bobl at aeolus.UUCP
Thu Jul 11 17:29:57 AEST 1985


I've got an interesting tool I hope to post to net.sources, but one of the
functions it calls, random(3), has been bothering me and I was hoping
someone familiar with the algorithm might be able to help me out.

The 4.2 documentation states:

	random()&01 will produce a random binary value

but it doesn't appear to until the algorithm "gets going".  In particular,
the first time random() is called after the initializing srandom(), the
result is always even.  The following program demonstrates this:
----------
#include <stdio.h>

main()
{
	int i;

	for (i = 0; i < 20; i++) {
		srandom(i);
		printf("%d\n", random());
	}

	exit(0);
}
----------
All of the resulting numbers are even.  Of course, the obvious workaround
is to "prime" the random number generator by calling random() a few (random
#?) times before actually using the numbers, but this begs the question:
Given that the low order bit of the first number is certainly not usable,
what is the minimum number of "primings" required before, as advertised,
"All bits generated by random are usable"?

Is there a better choice for the seed?  I've tried negative and large values
as well as the above with the same results.  In practice I hope to use
getpid().

I'll post results to net.math.

	- Bob L.



More information about the Comp.unix mailing list