author | Claus Gittinger <cg@exept.de> |
Wed, 07 May 2003 16:30:01 +0200 | |
changeset 1201 | 08004b394663 |
parent 1175 | 6c846afd9fbd |
child 1212 | 96dde1bb098d |
permissions | -rw-r--r-- |
65 | 1 |
" |
2 |
====================================================================== |
|
0 | 3 |
| |
4 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
5 |
| Written by Steve Byrne. |
|
6 |
| |
|
7 |
| This file is part of GNU Smalltalk. |
|
8 |
| |
|
9 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
10 |
| under the terms of the GNU General Public License as published by the Free |
|
11 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
12 |
| |
|
13 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
14 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
15 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
16 |
| details. |
|
17 |
| |
|
18 |
| You should have received a copy of the GNU General Public License along with |
|
19 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
20 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
| |
|
65 | 22 |
====================================================================== |
0 | 23 |
|
65 | 24 |
see notice in (Random>>documentation) |
0 | 25 |
" |
26 |
||
928 | 27 |
"{ Package: 'stx:libbasic2' }" |
28 |
||
0 | 29 |
Stream subclass:#Random |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
30 |
instanceVariableNames:'seed increment multiplier modulus' |
973
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
31 |
classVariableNames:'RandomGenerator RandomSalt' |
232 | 32 |
poolDictionaries:'' |
33 |
category:'Magnitude-Numbers' |
|
0 | 34 |
! |
35 |
||
65 | 36 |
!Random class methodsFor:'documentation'! |
0 | 37 |
|
31 | 38 |
copyright |
39 |
" |
|
40 |
====================================================================== |
|
41 |
| |
|
42 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
43 |
| Written by Steve Byrne. |
|
44 |
| |
|
45 |
| This file is part of GNU Smalltalk. |
|
46 |
| |
|
47 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
48 |
| under the terms of the GNU General Public License as published by the Free |
|
49 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
50 |
| |
|
51 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
52 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
53 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
54 |
| details. |
|
55 |
| |
|
56 |
| You should have received a copy of the GNU General Public License along with |
|
57 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
58 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
59 |
| |
|
60 |
====================================================================== |
|
61 |
||
62 |
see notice in (Random>>documentation) |
|
63 |
" |
|
64 |
! |
|
65 |
||
66 |
documentation |
|
67 |
" |
|
68 |
random numbers - thanks to Steves GNU Smalltalk |
|
947 | 69 |
|
842 | 70 |
This implements a linear congruential maximum period random number generator |
71 |
which passes the spectral test for randomness for dimensions 2 3 4 5 6. |
|
72 |
||
73 |
DO NOT USE IT FOR CRYPTOGRAPHY, because linear congruential generators |
|
74 |
are predictable and can be broken easily!! |
|
31 | 75 |
|
76 |
Notice: although being included here, |
|
258 | 77 |
this file is NOT covered by the ST/X license, but by |
78 |
the FSF copyLeft (see copyright method). |
|
31 | 79 |
|
258 | 80 |
You can redistribute it under the terms stated there ... |
81 |
Also, the price you pay for ST/X does not include a charge for |
|
82 |
this file - it has to be considered as a separate piece of |
|
83 |
software, which can be copied and given away without any |
|
84 |
restriction from my (CG) side. |
|
75 | 85 |
|
258 | 86 |
[author:] |
271 | 87 |
Steve Byrne |
258 | 88 |
Claus Gittinger |
947 | 89 |
|
90 |
[see also:] |
|
998 | 91 |
RandomTT800 - a new random generator |
92 |
RandomParkMiller - another new random generator |
|
271 | 93 |
" |
94 |
! |
|
95 |
||
96 |
examples |
|
97 |
" |
|
98 |
[exBegin] |
|
99 |
|rnd| |
|
258 | 100 |
|
271 | 101 |
rnd := Random new. |
102 |
10 timesRepeat:[ |
|
350 | 103 |
Transcript showCR:(rnd next) |
271 | 104 |
] |
105 |
[exEnd] |
|
106 |
||
107 |
rolling a dice: |
|
108 |
[exBegin] |
|
109 |
|rnd| |
|
110 |
||
111 |
rnd := Random new. |
|
112 |
10 timesRepeat:[ |
|
350 | 113 |
Transcript showCR:(rnd nextIntegerBetween:1 and:6) |
271 | 114 |
] |
115 |
[exEnd] |
|
31 | 116 |
" |
117 |
! ! |
|
0 | 118 |
|
65 | 119 |
!Random class methodsFor:'instance creation'! |
0 | 120 |
|
121 |
new |
|
17 | 122 |
"return a new random generator" |
123 |
||
0 | 124 |
^self basicNew setSeed |
842 | 125 |
! |
126 |
||
127 |
random |
|
128 |
"return a new random generator. |
|
129 |
Defined here for compatibility with StreamCipher" |
|
130 |
||
131 |
^ self new |
|
132 |
||
133 |
"Created: / 12.11.1999 / 17:52:08 / stefan" |
|
1128 | 134 |
! |
135 |
||
136 |
sharedGenerator |
|
137 |
"return a shared random generator." |
|
138 |
||
139 |
RandomGenerator isNil ifTrue:[ |
|
140 |
RandomGenerator := self new. |
|
141 |
]. |
|
142 |
^ RandomGenerator |
|
0 | 143 |
! ! |
144 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
145 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
146 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
147 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
148 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
149 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
150 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
151 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
152 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
153 |
|
1128 | 154 |
^ self sharedGenerator nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
155 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
156 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
157 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
158 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
159 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
160 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
161 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
162 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
163 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
164 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
165 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
166 |
|
851 | 167 |
nextBoolean |
168 |
"return a boolean random. |
|
169 |
This method behaves like the corresponding instance method, |
|
170 |
but allows generation of random numbers without |
|
171 |
a need for an instance of Random to be kept around. |
|
172 |
This uses a common, shared generator." |
|
173 |
||
1128 | 174 |
^ self sharedGenerator nextBoolean. |
851 | 175 |
|
176 |
" |
|
177 |
Transcript showCR:(Random nextBoolean). |
|
178 |
Transcript showCR:(Random nextBoolean). |
|
179 |
Transcript showCR:(Random nextBoolean). |
|
180 |
Transcript showCR:(Random nextBoolean). |
|
181 |
" |
|
182 |
||
183 |
"Created: 21.8.1997 / 18:08:23 / cg" |
|
184 |
! |
|
185 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
186 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
187 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
188 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
189 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
190 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
191 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
192 |
|
1128 | 193 |
^ self sharedGenerator nextInteger. |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
194 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
195 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
196 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
197 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
198 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
199 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
200 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
201 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
202 |
"Created: 21.8.1997 / 18:08:23 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
203 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
204 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
205 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
206 |
"return an integral random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
207 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
208 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
209 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
210 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
211 |
|
1128 | 212 |
^ self sharedGenerator nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
213 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
214 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
215 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
216 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
217 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
218 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
219 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
220 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
221 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
222 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
223 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
224 |
|
518 | 225 |
!Random class methodsFor:'testing'! |
226 |
||
793 | 227 |
bucketTest: randy |
228 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
229 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
230 |
indicates something is very wrong. Each run generates different values. |
|
231 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
232 |
go get coffee. |
|
233 |
This is a poor test; see Knuth. |
|
234 |
Some 'OK' runs: |
|
235 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
236 |
1011 987 982 980 982 974 968 1044 976 |
|
237 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
238 |
978 1035 995 988 1038 1009 988 993 976 |
|
239 |
" |
|
240 |
||
241 |
| nbuckets buckets ntrys slot | |
|
242 |
||
243 |
nbuckets := 20. |
|
244 |
buckets := Array new: nbuckets. |
|
245 |
buckets atAllPut: 0. |
|
246 |
ntrys := 1000. |
|
247 |
ntrys*nbuckets timesRepeat: [ |
|
248 |
slot := (randy next * nbuckets) floor + 1. |
|
249 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
250 |
Transcript cr. |
|
251 |
1 to: nbuckets do: [ :nb | |
|
252 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
253 |
||
254 |
||
255 |
"Execute this: |
|
256 |
Random bucketTest: Random new |
|
257 |
" |
|
258 |
||
259 |
! |
|
260 |
||
518 | 261 |
chiSquareTest |
262 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
263 |
o N = number of samples |
|
264 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
265 |
o Random number generator 'passes' if chisquare value is very close to r |
|
266 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
267 |
||
268 |
| aRand frequencies n range t chisquare | |
|
269 |
||
270 |
chisquare := Array new: 10. "Collect results from 10 trails" |
|
271 |
1 to: 10 do: [:k | "k = trail number" |
|
272 |
aRand := Random new. "Seeded differently each time" |
|
273 |
range := 100. |
|
274 |
n := 1000. |
|
275 |
frequencies := Array new: range. |
|
276 |
1 to: frequencies size do: [ :i | frequencies at: i put: 0 ]. |
|
277 |
1 to: n do: [ :i | |
|
278 |
t := ((aRand next) * range) truncated. |
|
279 |
frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ]. |
|
280 |
t := 0. |
|
281 |
1 to: range do: [ :i | |
|
282 |
t := t + ((frequencies at: i) squared) ]. |
|
283 |
chisquare at: k put: (((range * t / n) - n) asFloat). |
|
284 |
]. |
|
285 |
^ chisquare |
|
286 |
||
287 |
" |
|
288 |
Random chiSquareTest |
|
289 |
" |
|
290 |
||
291 |
" |
|
292 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
293 |
The closer to 100, the better. |
|
294 |
" |
|
295 |
||
296 |
"Modified: 16.4.1997 / 16:48:26 / cg" |
|
297 |
! ! |
|
298 |
||
1201 | 299 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 300 |
|
301 |
nextIntFrom:lowerBound to:upperBound |
|
302 |
"return a random integer in the given range" |
|
303 |
||
304 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
305 |
||
306 |
" |
|
307 |
Random new nextIntFrom:5 to:10 |
|
308 |
" |
|
309 |
! ! |
|
310 |
||
65 | 311 |
!Random methodsFor:'accessing-reading'! |
0 | 312 |
|
142 | 313 |
next |
314 |
"return the next random number in the range 0..1" |
|
17 | 315 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
316 |
^ self nextInteger / modulus asFloat |
17 | 317 |
|
318 |
"|r| |
|
319 |
r := Random new. |
|
350 | 320 |
Transcript showCR:r next. |
321 |
Transcript showCR:r next. |
|
322 |
Transcript showCR:r next. |
|
323 |
Transcript showCR:r next. |
|
17 | 324 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
325 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
326 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 327 |
! |
328 |
||
65 | 329 |
nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
330 |
"return a random number between start and stop. |
65 | 331 |
claus: the original GNU version has a bug in returning values |
332 |
from the interval [start .. stop+1]" |
|
333 |
||
334 |
|rnd| |
|
335 |
||
336 |
rnd := self next. |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
337 |
rnd := rnd * (stop - start) asFloat. |
65 | 338 |
rnd := rnd + start asFloat. |
339 |
^ rnd |
|
340 |
||
341 |
"|r| |
|
342 |
r := Random new. |
|
350 | 343 |
Transcript showCR:(r nextBetween:1 and:10). |
344 |
Transcript showCR:(r nextBetween:1 and:10). |
|
345 |
Transcript showCR:(r nextBetween:1 and:10). |
|
346 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 347 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
348 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
349 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 350 |
! |
351 |
||
0 | 352 |
nextBoolean |
17 | 353 |
"return true or false by random" |
354 |
||
851 | 355 |
^ self next < 0.5 |
17 | 356 |
|
357 |
"|r| |
|
358 |
r := Random new. |
|
350 | 359 |
Transcript showCR:r nextBoolean. |
360 |
Transcript showCR:r nextBoolean. |
|
361 |
Transcript showCR:r nextBoolean. |
|
362 |
Transcript showCR:r nextBoolean. |
|
17 | 363 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
364 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
365 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
366 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
367 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
368 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
369 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
370 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
371 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
372 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
373 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
374 |
|
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
375 |
"Modified: / 21.8.1998 / 14:45:55 / cg" |
0 | 376 |
! |
377 |
||
840 | 378 |
nextByte |
379 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
380 |
||
381 |
self step. |
|
382 |
^ seed bitAnd:16rFF |
|
383 |
||
384 |
" |
|
385 |
|r| |
|
386 |
r := Random new. |
|
387 |
Transcript showCR:r nextByte. |
|
388 |
Transcript showCR:r nextByte. |
|
389 |
Transcript showCR:r nextByte. |
|
390 |
Transcript showCR:r nextByte. |
|
391 |
" |
|
392 |
||
393 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
394 |
! |
|
395 |
||
396 |
nextBytes:count |
|
978 | 397 |
"return count random bytes (0..16rFF each)" |
840 | 398 |
|
399 |
|res| |
|
400 |
||
1175 | 401 |
res := ByteArray uninitializedNew:count. |
840 | 402 |
|
403 |
1 to:count do:[:i| |
|
404 |
self step. |
|
405 |
res at:i put:(seed bitAnd:16rFF). |
|
406 |
]. |
|
407 |
||
408 |
^ res |
|
409 |
||
410 |
" |
|
411 |
|r| |
|
412 |
r := Random new. |
|
413 |
Transcript showCR:(r nextBytes:20). |
|
414 |
" |
|
415 |
||
416 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
417 |
! |
|
418 |
||
142 | 419 |
nextInteger |
420 |
"return the next integral random number, |
|
421 |
in the range 0 .. 16r3FFFFFFF. |
|
422 |
From Sedgewick's 'Algorithms', based on Lehmer's method" |
|
423 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
424 |
self step. |
142 | 425 |
^ seed |
426 |
||
840 | 427 |
" |
428 |
|r| |
|
142 | 429 |
r := Random new. |
350 | 430 |
Transcript showCR:r nextInteger. |
431 |
Transcript showCR:r nextInteger. |
|
432 |
Transcript showCR:r nextInteger. |
|
433 |
Transcript showCR:r nextInteger. |
|
142 | 434 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
435 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
436 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 437 |
! |
438 |
||
17 | 439 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
440 |
"return an integral random number between start and stop" |
17 | 441 |
|
442 |
|rnd| |
|
443 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
444 |
rnd := self next. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
445 |
rnd := rnd * ((stop - start) asFloat + 1.0). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
446 |
^ rnd truncated + start |
17 | 447 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
448 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
449 |
|r| |
17 | 450 |
r := Random new. |
350 | 451 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
452 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
453 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
454 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 455 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
456 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
457 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
458 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
459 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
460 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
461 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
462 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
463 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
464 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
465 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
466 |
|
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
467 |
"Modified: / 21.8.1998 / 14:45:04 / cg" |
0 | 468 |
! |
469 |
||
470 |
nextMatchFor: aNumber |
|
17 | 471 |
"generate the next random, return true iff it has the same |
472 |
value as aNumber. Redefined to avoid endless reading." |
|
473 |
||
0 | 474 |
^self next = aNumber |
475 |
! ! |
|
476 |
||
65 | 477 |
!Random methodsFor:'blocked methods'! |
0 | 478 |
|
65 | 479 |
contents |
480 |
"blocked from use - contents makes no sense for random generators" |
|
481 |
||
482 |
self shouldNotImplement |
|
483 |
! |
|
484 |
||
485 |
nextPut: value |
|
486 |
"blocked from use - it makes no sense for randoms" |
|
487 |
||
488 |
self shouldNotImplement |
|
489 |
! ! |
|
490 |
||
491 |
!Random methodsFor:'private'! |
|
0 | 492 |
|
493 |
setSeed |
|
842 | 494 |
"set the initial seed value based on the current time. |
495 |
These numbers implement a maximum period generator which passes |
|
496 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
497 |
the product does not overflow 2 raisedTo:29. |
|
498 |
||
499 |
These numbers are carefully choosen, so don't change them, |
|
500 |
unless you know what you are doing!!" |
|
17 | 501 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
502 |
seed := Time millisecondClockValue bitAnd:16rFFFF. |
973
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
503 |
RandomSalt isNil ifTrue:[ |
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
504 |
RandomSalt := 1. |
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
505 |
] ifFalse:[ |
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
506 |
RandomSalt := RandomSalt + 1. |
973
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
507 |
]. |
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
508 |
seed := seed + RandomSalt. |
842 | 509 |
multiplier := 1597. |
510 |
increment := 51749. |
|
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
511 |
modulus := 244944 " 244957 " . |
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
512 |
|
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
513 |
self step. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
514 |
|
842 | 515 |
"Modified: / 1.4.1997 / 22:44:13 / cg" |
516 |
"Modified: / 12.11.1999 / 17:50:52 / stefan" |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
517 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
518 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
519 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
520 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
521 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
522 |
"/ seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
523 |
seed := seed * multiplier + increment \\ modulus |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
524 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
525 |
"Created: 1.4.1997 / 22:40:45 / cg" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
526 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 527 |
! ! |
65 | 528 |
|
142 | 529 |
!Random methodsFor:'testing'! |
530 |
||
531 |
atEnd |
|
532 |
"instances of Random can always give more numbers" |
|
533 |
||
534 |
^ false |
|
535 |
! |
|
536 |
||
537 |
isReadable |
|
538 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
539 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
540 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
541 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
542 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
543 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
544 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 545 |
! ! |
546 |
||
547 |
!Random class methodsFor:'documentation'! |
|
548 |
||
549 |
version |
|
1201 | 550 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.34 2003-05-07 14:30:01 cg Exp $' |
142 | 551 |
! ! |