Bug in random number generator
tektronix!zehntel!sytek!menlo70!hao!seismo!rocheste!ritcv!kar
tektronix!zehntel!sytek!menlo70!hao!seismo!rocheste!ritcv!kar
Tue Mar 15 14:09:21 AEST 1983
The random number generator that came with our 4.1BSD includes the
following line of code (that does the work):
return((randx = randx * 1103515245 + 12345) & 0x7fffffff);
and the generator on our old V7 system includes this line of code:
return((randx = randx * 1103515245 + 12345)>>16 & 0x7fff);
The V7 version produced random numbers in the range 0-32767, and the new ver-
sion produces numbers in the range 0-2147483647.
Unfortunately, the numbers produced by the new version are NOT random.
To verify my claim, try the following program:
for(;;)
printf( "%d\n", rand() % 2 );
and see what happens. If you mod your "random" numbers by any power of 2,
you'll be disappointed with the results.
The fix? Go back to the V7 generator. The algorithm used produces
numbers that are not very random in the low order bits; this is the real
reason for the shift and mask to 16 bits, not the 16 bit architecture of the
PDP-11.
- Ken Reek, Rochester Institute of Technology
ucbvax!allegra!rochester!ritcv!kar
More information about the Comp.bugs.4bsd.ucb-fixes
mailing list