More on Random; a smaller faster P-M for Squeak
David N. Smith (IBM)
dnsmith at watson.ibm.com
Fri Jun 11 22:19:16 UTC 1999
At 17:06 -0400 6/11/99, David N. Smith \(IBM\) wrote:
>...SNIP...
>BTW, on rereading the P-M paper, I 'discovered' that there is a simpler f-p
>version which works with IEEE floats. The version we're using is converted
>from C and was designed with a 32-bit word in mind. While it matches one in
>the paper (there are four versions given), it's not the simplest case for
>Squeak. Maybe next week ... sigh...
If anyone wants to play, here is that other version. It's faster by a third
and a lot smaller and simpler.
initialize
" Set a reasonable Park-Miller starting seed "
seed := ((Time millisecondClockValue bitXor: self hash)
bitAnd: 16r7FFFFFFC) + 1.
a := 16r000041A7 asFloat. " magic constant = 16807 "
m := 16r7FFFFFFF asFloat. " magic constant = 2147483647 "
next
" Generate random instances of Float in the interval 0 to 1.
The algorithm is described in detail in 'Random Number Generators:
Good Ones Are Hard to Find' by Stephen K. Park and Keith W. Miller
(Comm. Asso. Comp. Mach., 31(10):1192--1201, 1988). "
| temp |
temp := a * seed.
seed := temp - (m * (temp // m)).
^ seed / m
Remove #nextValue and add these two methods. The instance variables "q" and
"r" are no longer used.
Warning: I've just written these. They match the code in the original P-M
paper. They pass the two tests in the class. I've generated 100,000 values
once. Buyer beware.
The sequence of values for a given starting seed should be identical with
the old version.
BTW, that line which computes "seed" is this in Pascal in the paper:
seed := temp - m * Trunc(temp / m);
Dave
_______________________________
David N. Smith
IBM T J Watson Research Center
Hawthorne, NY
_______________________________
Any opinions or recommendations
herein are those of the author
and not of his employer.
More information about the Squeak-dev
mailing list
|