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