[comp.sys.mac] Misuse of random number generators

bill@utastro.UUCP (William H. Jefferys) (12/26/87)

In the discussion of Chris Christensen's problem with a macro and the
random number generator, everyone seems to have missed a key point. All
the answers pointed out the problems with Chris' use of the macro, such 
as Rich Siegel's response (not to pick on Rich):

~A correct usage would be:
~
~	x = Random();
~	x = ABS(x) % 8;

Although this does correct the incorrect macro use, it leaves another
potential problem unsolved.  I believe that the random number
generator is of the multiplicative congruential type, and that
the low order bits of such a generator are not guaranteed to give 
a very good approximation to a sequence of random numbers. Only the 
high order bits can be relied upon as a good approximation to a random
sequence.  Reduction modulo n, or (a variation) ANDing with a low-bit 
mask uses only the low order bits and may give a poor sequence. It 
would be much better to use a shift to capture the high order bits.

Thus if Random() is supplying bit patterns that fill up a longint, 
the best way to get the desired sequence is

	x = Random() >> 24;
	x = ABS(x);

Bill Jefferys
-- 
Glend.	I can call spirits from the vasty deep.
Hot.	Why, so can I, or so can any man; But will they come when you
	do call for them?    --  Henry IV Pt. I, III, i, 53

jmm@thoth8.berkeley.edu.BERKELEY.EDU (12/31/87)

If this is true, what's the best way to get random numbers in the range
1..6?  (I'm using LSPascal.)

Thanks,

James Moore
-------------------------	|
/ Chomh h-ard le balla	/    	|  jmm@bartleby.berekely.edu
/ Chomh ban le bainne	/	|--------------------------------------------|
/ Chomh dearg le fuil	/	|   The University of California only knows  |
/ Chomh milis le mil	/	|   me as a number.  They couldn't care less |
/ Ceard e seo?		/	|   what my opinions are.                    |
-------------------------	|--------------------------------------------|

bill@utastro.UUCP (William H. Jefferys) (01/01/88)

In article <6418@jade.BERKELEY.EDU> jmm@thoth8.berkeley.edu.BERKELEY.EDU () writes:
~If this is true, what's the best way to get random numbers in the range
~1..6?  (I'm using LSPascal.)
~
Use a linear transformation. If the built-in RNG gives numbers
distributed uniformly on [x0,x1] and you want them distributed
uniformly on [y0,y1] then let

	y := (x - x0)*(y1 - y0)/(x1 - x0) + y0;

The special case when y0 = 0, y1 = 1 is particularly useful since this
is the usual starting point for obtaining other distributions (e.g.,
Gaussian, Poisson, ...)

Bill Jefferys
-- 
Glend.	I can call spirits from the vasty deep.
Hot.	Why, so can I, or so can any man; But will they come when you
	do call for them?    --  Henry IV Pt. I, III, i, 53

bill@utastro.UUCP (William H. Jefferys) (01/01/88)

In article <6418@jade.BERKELEY.EDU> jmm@thoth8.berkeley.edu.BERKELEY.EDU () writes:
~If this is true, what's the best way to get random numbers in the range
~1..6?  (I'm using LSPascal.)
~
I forgot to mention, you need to be sure that the calculation is done in
real arithmetic so you don't have integer divides. Also, if you need
integer results (i.e., you are simulating a die, which would be likely
given the above question) just make sure you have the right interval
and truncate the result. Be careful about the end points, though.

Bill Jefferys
-- 
Glend.	I can call spirits from the vasty deep.
Hot.	Why, so can I, or so can any man; But will they come when you
	do call for them?    --  Henry IV Pt. I, III, i, 53