2072
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#RandomParkMillerUsingFloat
|
|
4 |
instanceVariableNames:'seed a m q r'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Magnitude-Numbers'
|
|
8 |
!
|
|
9 |
|
|
10 |
!RandomParkMillerUsingFloat class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Like RandomParkMiller, but using floats (instead of LargeInts).
|
|
15 |
This makes this one roughly 4 times faster.
|
|
16 |
|
|
17 |
Please read:
|
|
18 |
Standard reference by Park and Miller in
|
|
19 |
'Random Number Generators: Good Ones Are Hard to Find',
|
|
20 |
Comm. ACM, 31:1192-1201, 1988.
|
|
21 |
|
|
22 |
[see also:]
|
|
23 |
Random - fast, but generates less quality random numbers
|
|
24 |
RandomTT800 - another random generator
|
|
25 |
"
|
|
26 |
!
|
|
27 |
|
|
28 |
testing
|
|
29 |
"
|
|
30 |
|
|
31 |
|r|
|
|
32 |
|
|
33 |
r := self new.
|
|
34 |
(1 to:10) collect:[:i | r next]
|
|
35 |
|
|
36 |
-> should be
|
|
37 |
#(
|
|
38 |
0.1492432697
|
|
39 |
0.3316330217
|
|
40 |
0.7561964480
|
|
41 |
0.3937015400
|
|
42 |
0.9417831814
|
|
43 |
0.5499291939
|
|
44 |
0.6599625962
|
|
45 |
0.9913545591
|
|
46 |
0.6960744326
|
|
47 |
0.9229878997
|
|
48 |
#)
|
|
49 |
"
|
|
50 |
! !
|
|
51 |
|
|
52 |
!RandomParkMillerUsingFloat class methodsFor:'initialization'!
|
|
53 |
|
|
54 |
initialize
|
|
55 |
PMa := 16807.
|
|
56 |
PMm := 2147483647. " magic constant = 16807 "
|
|
57 |
PMq := 127773. " magic constant = 2147483647 "
|
|
58 |
PMr := 2836. " quotient (m quo: a) = 44488 "
|
|
59 |
PMmu1 := 4.65661E-10 " remainder (m \\ a). = 2836 "
|
|
60 |
! !
|
|
61 |
|
|
62 |
!RandomParkMillerUsingFloat class methodsFor:'instance creation'!
|
|
63 |
|
|
64 |
new
|
|
65 |
self initialize.
|
|
66 |
^ super new initialize
|
|
67 |
! !
|
|
68 |
|
|
69 |
!RandomParkMillerUsingFloat methodsFor:'accessing-reading'!
|
|
70 |
|
|
71 |
next
|
|
72 |
" This method generates random instances of Float in the interval 0.0 to 1.0 "
|
|
73 |
|
|
74 |
seed := self peekValue.
|
|
75 |
^ seed / m
|
|
76 |
!
|
|
77 |
|
|
78 |
nextInteger
|
|
79 |
" This method generates random instances of Integer in the interval 0 to 16r7FFFFFFF. "
|
|
80 |
|
|
81 |
seed := self peekValue.
|
|
82 |
^ seed
|
|
83 |
|
|
84 |
"
|
|
85 |
self new next
|
|
86 |
self new nextInteger
|
|
87 |
"
|
|
88 |
! !
|
|
89 |
|
|
90 |
!RandomParkMillerUsingFloat methodsFor:'initialization'!
|
|
91 |
|
|
92 |
initialize
|
|
93 |
" Set a reasonable Park-Miller starting seed "
|
|
94 |
|
|
95 |
[
|
|
96 |
seed := (Time millisecondClockValue bitAnd: 16r3FFFFFFF) bitXor: self hash.
|
|
97 |
seed = 0
|
|
98 |
] whileTrue: ["Try again if ever get a seed = 0"].
|
|
99 |
|
|
100 |
a := 16r000041A7 asFloat. " magic constant = 16807 "
|
|
101 |
m := 16r7FFFFFFF asFloat. " magic constant = 2147483647 "
|
|
102 |
q := (m quo: a) asFloat.
|
|
103 |
r := (m \\ a) asFloat.
|
|
104 |
!
|
|
105 |
|
|
106 |
seed:anInteger
|
|
107 |
seed := anInteger
|
|
108 |
! !
|
|
109 |
|
|
110 |
!RandomParkMillerUsingFloat methodsFor:'private'!
|
|
111 |
|
|
112 |
peek
|
|
113 |
" This method answers the next random number that will be generated as a Float in the range [0..1).
|
|
114 |
It answers the same value for all successive message sends. "
|
|
115 |
|
|
116 |
^ self peekValue / m
|
|
117 |
!
|
|
118 |
|
|
119 |
peekValue
|
|
120 |
"This method generates random instances of Integer in the interval
|
|
121 |
0 to 16r7FFFFFFF. This method does NOT update the seed; repeated sends
|
|
122 |
answer the same value.
|
|
123 |
The algorithm is described in detail in 'Random Number Generators:
|
|
124 |
Good Ones Are Hard to Find' by Stephen K. Park and Keith W. Miller
|
|
125 |
(Comm. Asso. Comp. Mach., 31(10):1192--1201, 1988)."
|
|
126 |
|
|
127 |
|lo hi aLoRHi|
|
|
128 |
|
|
129 |
hi := (seed quo:q) asFloat.
|
|
130 |
lo := seed - (hi * q).
|
|
131 |
aLoRHi := (a * lo) - (r * hi).
|
|
132 |
(aLoRHi > 0) ifTrue:[ ^ aLoRHi ].
|
|
133 |
^ aLoRHi + m
|
|
134 |
! !
|
|
135 |
|
|
136 |
!RandomParkMillerUsingFloat class methodsFor:'documentation'!
|
|
137 |
|
|
138 |
version
|
|
139 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomParkMillerUsingFloat.st,v 1.1 2008-12-16 22:39:51 cg Exp $'
|
|
140 |
! !
|
|
141 |
|
|
142 |
RandomParkMillerUsingFloat initialize!
|