Re: Mini challenge - coin toss Message #13 Posted by Andrés C. Rodríguez (Argentina) on 22 Mar 2009, 5:31 p.m., in response to message #1 by MikeO
While it is possible to use the linear congruential method to create pseudorandom sequences of the form
x(i) = FRAC ((x(i-1) * a + b)
as some of the answers to this challenge suggested, not every value for "a" and "b" gives acceptable random behavior.
The matter is throughly discussed in Knuth's "Art of Computer Programming", Vol 2. There, you can find a nice example of a complex and apparently good random generator which, after a few cycles, begins to oscillate within a few answers that repeat themselves in an obviously non-random manner.
Knuth dixit: "Never use a random method to generate random numbers"
One of the better tests for randomness is the "Spectral test", also discussed in the same book.
HP took care, when creating pseudorandom generators, in the HP 25 Applications Manual, and in the HP 41 Standard Pac, to publish programs that pass the Spectral test. I have long memorized them, and used them from time to time. While they are not the shortest, they are short (and fast) enough for usual purposes.
For the HP 41:
RCL 00
9821
*
.211327
+
FRAC
STO oo
For the coin toss, simply add:
2
*
INT
(An answer of 0 is "heads", an answer of 1 is "tails")
Of course, the pseudorandom "seed" can be any number between 0 and 1, stored in Register 00
|