977
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#RandomParkMiller
|
|
4 |
instanceVariableNames:'seed'
|
|
5 |
classVariableNames:'PMa PMm PMmu1 PMq PMr'
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Magnitude-Numbers'
|
|
8 |
!
|
|
9 |
|
|
10 |
!RandomParkMiller class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
NO WARRANTY
|
|
15 |
|
|
16 |
Another pseudo-random number generator
|
|
17 |
|
2427
|
18 |
The ParkMiller random generator (although better than the old Random), is not recommended
|
|
19 |
when a high quality random is required (for example, for cryptographic work).
|
|
20 |
Applications should use either the OS-random generator or a LaggedFibonacci generator.
|
|
21 |
This is because the random values provided by the Park-Miller generator are double precision
|
|
22 |
floating point numbers which have up to 53 significant bits. Since only the first 31 bits
|
|
23 |
of their mantissa are known to have good random properties, the behavior of the remaining
|
|
24 |
22 bits is undefined.
|
|
25 |
In particular, bit aliasing occurs during the calculation of the next random value,
|
|
26 |
and bit 22 of the mantissa is always 1.
|
|
27 |
|
3349
|
28 |
Warning: this generator should not be used for cryptographic work.
|
|
29 |
|
998
|
30 |
Please read:
|
|
31 |
Standard reference by Park and Miller in
|
|
32 |
'Random Number Generators: Good Ones Are Hard to Find',
|
|
33 |
Comm. ACM, 31:1192-1201, 1988.
|
|
34 |
|
|
35 |
[see also:]
|
|
36 |
Random - fast, but generates less quality random numbers
|
|
37 |
RandomTT800 - another random generator
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
testing
|
|
42 |
"
|
|
43 |
|
977
|
44 |
|r|
|
|
45 |
|
|
46 |
r := self new.
|
|
47 |
(1 to:10) collect:[:i | r next]
|
|
48 |
|
|
49 |
-> should be
|
|
50 |
#(
|
|
51 |
0.1492432697
|
|
52 |
0.3316330217
|
|
53 |
0.7561964480
|
|
54 |
0.3937015400
|
|
55 |
0.9417831814
|
|
56 |
0.5499291939
|
|
57 |
0.6599625962
|
|
58 |
0.9913545591
|
|
59 |
0.6960744326
|
|
60 |
0.9229878997
|
|
61 |
#)
|
|
62 |
"
|
|
63 |
! !
|
|
64 |
|
|
65 |
!RandomParkMiller class methodsFor:'initialization'!
|
|
66 |
|
|
67 |
initialize
|
2073
|
68 |
PMa := 16807. " magic constant "
|
|
69 |
PMm := 2147483647. " magic constant "
|
|
70 |
PMq := 127773. " quotient (m quo: a) = 44488 "
|
|
71 |
PMr := 2836. " remainder (m \\ a). = 2836 "
|
|
72 |
PMmu1 := 4.65661E-10
|
977
|
73 |
! !
|
|
74 |
|
|
75 |
!RandomParkMiller class methodsFor:'instance creation'!
|
|
76 |
|
|
77 |
new
|
|
78 |
self initialize.
|
|
79 |
^ super new initialize
|
|
80 |
! !
|
|
81 |
|
|
82 |
!RandomParkMiller methodsFor:'accessing-reading'!
|
|
83 |
|
|
84 |
next
|
|
85 |
" This method generates random instances of Float in the interval 0.0 to 1.0 "
|
|
86 |
|
|
87 |
seed := self nextInteger.
|
|
88 |
^ seed * PMmu1
|
|
89 |
!
|
|
90 |
|
3338
|
91 |
nextBoolean
|
|
92 |
" This method generates a boolean "
|
|
93 |
|
|
94 |
^ self next > 0.5
|
|
95 |
!
|
|
96 |
|
977
|
97 |
nextInteger
|
|
98 |
" This method generates random instances of Integer in the interval 0 to 16r7FFFFFFF. "
|
|
99 |
|
|
100 |
seed := self peekInteger.
|
|
101 |
^ seed
|
|
102 |
! !
|
|
103 |
|
|
104 |
!RandomParkMiller methodsFor:'initialization'!
|
|
105 |
|
|
106 |
initialize
|
|
107 |
" Set a reasonable Park-Miller starting seed "
|
|
108 |
|
|
109 |
seed := 2345678901
|
|
110 |
!
|
|
111 |
|
|
112 |
seed:anInteger
|
|
113 |
seed := anInteger
|
|
114 |
! !
|
|
115 |
|
|
116 |
!RandomParkMiller methodsFor:'private'!
|
|
117 |
|
|
118 |
peek
|
2073
|
119 |
" This method answers the next random number that will be generated as a Float in the range [0..1).
|
|
120 |
It answers the same value for all successive message sends. "
|
977
|
121 |
|
|
122 |
^ self peekInteger * PMmu1
|
|
123 |
!
|
|
124 |
|
|
125 |
peekInteger
|
|
126 |
" This method generates random instances of Integer in the interval 0 to 16r7FFFFFFF. This method does NOT update the seed; repeated sends answer the same value. 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). "
|
|
127 |
|
|
128 |
|lo hi aLoRHi answer|
|
|
129 |
|
|
130 |
hi := seed quo:PMq.
|
|
131 |
lo := seed rem:PMq.
|
|
132 |
aLoRHi := (PMa * lo) - (PMr * hi).
|
|
133 |
(aLoRHi > 0) ifTrue:[ ^ aLoRHi ].
|
|
134 |
^ aLoRHi + PMm
|
|
135 |
! !
|
|
136 |
|
|
137 |
!RandomParkMiller class methodsFor:'documentation'!
|
|
138 |
|
|
139 |
version
|
3349
|
140 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomParkMiller.st,v 1.6 2014-10-01 12:48:00 cg Exp $'
|
2427
|
141 |
!
|
|
142 |
|
|
143 |
version_CVS
|
3349
|
144 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomParkMiller.st,v 1.6 2014-10-01 12:48:00 cg Exp $'
|
977
|
145 |
! !
|
2073
|
146 |
|
3338
|
147 |
|
977
|
148 |
RandomParkMiller initialize!
|