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 |
|
998
|
18 |
Please read:
|
|
19 |
Standard reference by Park and Miller in
|
|
20 |
'Random Number Generators: Good Ones Are Hard to Find',
|
|
21 |
Comm. ACM, 31:1192-1201, 1988.
|
|
22 |
|
|
23 |
[see also:]
|
|
24 |
Random - fast, but generates less quality random numbers
|
|
25 |
RandomTT800 - another random generator
|
|
26 |
"
|
|
27 |
!
|
|
28 |
|
|
29 |
testing
|
|
30 |
"
|
|
31 |
|
977
|
32 |
|r|
|
|
33 |
|
|
34 |
r := self new.
|
|
35 |
(1 to:10) collect:[:i | r next]
|
|
36 |
|
|
37 |
-> should be
|
|
38 |
#(
|
|
39 |
0.1492432697
|
|
40 |
0.3316330217
|
|
41 |
0.7561964480
|
|
42 |
0.3937015400
|
|
43 |
0.9417831814
|
|
44 |
0.5499291939
|
|
45 |
0.6599625962
|
|
46 |
0.9913545591
|
|
47 |
0.6960744326
|
|
48 |
0.9229878997
|
|
49 |
#)
|
|
50 |
"
|
|
51 |
! !
|
|
52 |
|
|
53 |
!RandomParkMiller class methodsFor:'initialization'!
|
|
54 |
|
|
55 |
initialize
|
|
56 |
PMa := 16807.
|
|
57 |
PMm := 2147483647. " magic constant = 16807 "
|
|
58 |
PMq := 127773. " magic constant = 2147483647 "
|
|
59 |
PMr := 2836. " quotient (m quo: a) = 44488 "
|
|
60 |
PMmu1 := 4.65661E-10 " remainder (m \\ a). = 2836 "
|
|
61 |
! !
|
|
62 |
|
|
63 |
!RandomParkMiller class methodsFor:'instance creation'!
|
|
64 |
|
|
65 |
new
|
|
66 |
self initialize.
|
|
67 |
^ super new initialize
|
|
68 |
! !
|
|
69 |
|
|
70 |
!RandomParkMiller methodsFor:'accessing-reading'!
|
|
71 |
|
|
72 |
next
|
|
73 |
" This method generates random instances of Float in the interval 0.0 to 1.0 "
|
|
74 |
|
|
75 |
seed := self nextInteger.
|
|
76 |
^ seed * PMmu1
|
|
77 |
!
|
|
78 |
|
|
79 |
nextInteger
|
|
80 |
" This method generates random instances of Integer in the interval 0 to 16r7FFFFFFF. "
|
|
81 |
|
|
82 |
seed := self peekInteger.
|
|
83 |
^ seed
|
|
84 |
! !
|
|
85 |
|
|
86 |
!RandomParkMiller methodsFor:'initialization'!
|
|
87 |
|
|
88 |
initialize
|
|
89 |
" Set a reasonable Park-Miller starting seed "
|
|
90 |
|
|
91 |
seed := 2345678901
|
|
92 |
!
|
|
93 |
|
|
94 |
seed:anInteger
|
|
95 |
seed := anInteger
|
|
96 |
! !
|
|
97 |
|
|
98 |
!RandomParkMiller methodsFor:'private'!
|
|
99 |
|
|
100 |
peek
|
|
101 |
" This method answers the next random number that will be generated as a Float in the range [0..1). It answers the same value for all successive message sends. "
|
|
102 |
|
|
103 |
^ self peekInteger * PMmu1
|
|
104 |
!
|
|
105 |
|
|
106 |
peekInteger
|
|
107 |
" 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). "
|
|
108 |
|
|
109 |
|lo hi aLoRHi answer|
|
|
110 |
|
|
111 |
hi := seed quo:PMq.
|
|
112 |
lo := seed rem:PMq.
|
|
113 |
aLoRHi := (PMa * lo) - (PMr * hi).
|
|
114 |
(aLoRHi > 0) ifTrue:[ ^ aLoRHi ].
|
|
115 |
^ aLoRHi + PMm
|
|
116 |
! !
|
|
117 |
|
|
118 |
!RandomParkMiller class methodsFor:'documentation'!
|
|
119 |
|
|
120 |
version
|
998
|
121 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomParkMiller.st,v 1.2 2001-11-17 11:08:33 cg Exp $'
|
977
|
122 |
! !
|
|
123 |
RandomParkMiller initialize!
|