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