author | Stefan Vogel <sv@exept.de> |
Wed, 24 Jan 2007 16:27:00 +0100 | |
changeset 1841 | fe14c3e736ef |
parent 1642 | 0abc34691f6c |
child 1878 | 07b7aa1394c6 |
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 |
||
135 |
sharedGenerator |
|
136 |
"return a shared random generator." |
|
137 |
||
138 |
RandomGenerator isNil ifTrue:[ |
|
139 |
RandomGenerator := self new. |
|
140 |
]. |
|
141 |
^ RandomGenerator |
|
0 | 142 |
! ! |
143 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
144 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
145 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
146 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
147 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
148 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
149 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
150 |
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
|
151 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
152 |
|
1128 | 153 |
^ self sharedGenerator nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
154 |
|
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 |
Transcript showCR:(Random nextBetween:1 and:100). |
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 |
" |
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 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
163 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
164 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
165 |
|
851 | 166 |
nextBoolean |
167 |
"return a boolean random. |
|
168 |
This method behaves like the corresponding instance method, |
|
169 |
but allows generation of random numbers without |
|
170 |
a need for an instance of Random to be kept around. |
|
171 |
This uses a common, shared generator." |
|
172 |
||
1128 | 173 |
^ self sharedGenerator nextBoolean. |
851 | 174 |
|
175 |
" |
|
176 |
Transcript showCR:(Random nextBoolean). |
|
177 |
Transcript showCR:(Random nextBoolean). |
|
178 |
Transcript showCR:(Random nextBoolean). |
|
179 |
Transcript showCR:(Random nextBoolean). |
|
180 |
" |
|
181 |
||
182 |
"Created: 21.8.1997 / 18:08:23 / cg" |
|
183 |
! |
|
184 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
185 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
186 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
187 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
188 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
189 |
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
|
190 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
191 |
|
1128 | 192 |
^ self sharedGenerator nextInteger. |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
193 |
|
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 |
Transcript showCR:(Random nextInteger). |
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 |
" |
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 |
"Created: 21.8.1997 / 18:08:23 / cg" |
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 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
204 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
205 |
"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
|
206 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
207 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
208 |
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
|
209 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
210 |
|
1128 | 211 |
^ self sharedGenerator nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
212 |
|
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 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
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 |
" |
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 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
221 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
222 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
223 |
|
518 | 224 |
!Random class methodsFor:'testing'! |
225 |
||
793 | 226 |
bucketTest: randy |
227 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
228 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
229 |
indicates something is very wrong. Each run generates different values. |
|
230 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
231 |
go get coffee. |
|
232 |
This is a poor test; see Knuth. |
|
233 |
Some 'OK' runs: |
|
234 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
235 |
1011 987 982 980 982 974 968 1044 976 |
|
236 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
237 |
978 1035 995 988 1038 1009 988 993 976 |
|
238 |
" |
|
239 |
||
240 |
| nbuckets buckets ntrys slot | |
|
241 |
||
242 |
nbuckets := 20. |
|
243 |
buckets := Array new: nbuckets. |
|
244 |
buckets atAllPut: 0. |
|
245 |
ntrys := 1000. |
|
246 |
ntrys*nbuckets timesRepeat: [ |
|
247 |
slot := (randy next * nbuckets) floor + 1. |
|
248 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
249 |
Transcript cr. |
|
250 |
1 to: nbuckets do: [ :nb | |
|
251 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
252 |
||
253 |
||
254 |
"Execute this: |
|
255 |
Random bucketTest: Random new |
|
256 |
" |
|
257 |
||
258 |
! |
|
259 |
||
518 | 260 |
chiSquareTest |
261 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
262 |
o N = number of samples |
|
263 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
264 |
o Random number generator 'passes' if chisquare value is very close to r |
|
265 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
266 |
||
267 |
| aRand frequencies n range t chisquare | |
|
268 |
||
269 |
chisquare := Array new: 10. "Collect results from 10 trails" |
|
270 |
1 to: 10 do: [:k | "k = trail number" |
|
271 |
aRand := Random new. "Seeded differently each time" |
|
272 |
range := 100. |
|
273 |
n := 1000. |
|
274 |
frequencies := Array new: range. |
|
275 |
1 to: frequencies size do: [ :i | frequencies at: i put: 0 ]. |
|
276 |
1 to: n do: [ :i | |
|
277 |
t := ((aRand next) * range) truncated. |
|
278 |
frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ]. |
|
279 |
t := 0. |
|
280 |
1 to: range do: [ :i | |
|
281 |
t := t + ((frequencies at: i) squared) ]. |
|
282 |
chisquare at: k put: (((range * t / n) - n) asFloat). |
|
283 |
]. |
|
284 |
^ chisquare |
|
285 |
||
286 |
" |
|
287 |
Random chiSquareTest |
|
288 |
" |
|
289 |
||
290 |
" |
|
291 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
292 |
The closer to 100, the better. |
|
293 |
" |
|
294 |
||
295 |
"Modified: 16.4.1997 / 16:48:26 / cg" |
|
296 |
! ! |
|
297 |
||
1201 | 298 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 299 |
|
300 |
nextIntFrom:lowerBound to:upperBound |
|
301 |
"return a random integer in the given range" |
|
302 |
||
303 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
304 |
||
305 |
" |
|
306 |
Random new nextIntFrom:5 to:10 |
|
307 |
" |
|
308 |
! ! |
|
309 |
||
65 | 310 |
!Random methodsFor:'accessing-reading'! |
0 | 311 |
|
142 | 312 |
next |
313 |
"return the next random number in the range 0..1" |
|
17 | 314 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
315 |
^ self nextInteger / modulus asFloat |
17 | 316 |
|
317 |
"|r| |
|
318 |
r := Random new. |
|
350 | 319 |
Transcript showCR:r next. |
320 |
Transcript showCR:r next. |
|
321 |
Transcript showCR:r next. |
|
322 |
Transcript showCR:r next. |
|
17 | 323 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
324 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
325 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 326 |
! |
327 |
||
65 | 328 |
nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
329 |
"return a random number between start and stop. |
65 | 330 |
claus: the original GNU version has a bug in returning values |
331 |
from the interval [start .. stop+1]" |
|
332 |
||
333 |
|rnd| |
|
334 |
||
335 |
rnd := self next. |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
336 |
rnd := rnd * (stop - start) asFloat. |
65 | 337 |
rnd := rnd + start asFloat. |
338 |
^ rnd |
|
339 |
||
340 |
"|r| |
|
341 |
r := Random new. |
|
350 | 342 |
Transcript showCR:(r nextBetween:1 and:10). |
343 |
Transcript showCR:(r nextBetween:1 and:10). |
|
344 |
Transcript showCR:(r nextBetween:1 and:10). |
|
345 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 346 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
347 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
348 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 349 |
! |
350 |
||
0 | 351 |
nextBoolean |
17 | 352 |
"return true or false by random" |
353 |
||
851 | 354 |
^ self next < 0.5 |
17 | 355 |
|
356 |
"|r| |
|
357 |
r := Random new. |
|
350 | 358 |
Transcript showCR:r nextBoolean. |
359 |
Transcript showCR:r nextBoolean. |
|
360 |
Transcript showCR:r nextBoolean. |
|
361 |
Transcript showCR:r nextBoolean. |
|
17 | 362 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
363 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
364 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
365 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
366 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
367 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
368 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
369 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
370 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
371 |
Transcript showCR:bag contents |
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 |
|
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
374 |
"Modified: / 21.8.1998 / 14:45:55 / cg" |
0 | 375 |
! |
376 |
||
840 | 377 |
nextByte |
378 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
379 |
||
380 |
self step. |
|
381 |
^ seed bitAnd:16rFF |
|
382 |
||
383 |
" |
|
384 |
|r| |
|
385 |
r := Random new. |
|
386 |
Transcript showCR:r nextByte. |
|
387 |
Transcript showCR:r nextByte. |
|
388 |
Transcript showCR:r nextByte. |
|
389 |
Transcript showCR:r nextByte. |
|
390 |
" |
|
391 |
||
392 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
393 |
! |
|
394 |
||
395 |
nextBytes:count |
|
978 | 396 |
"return count random bytes (0..16rFF each)" |
840 | 397 |
|
398 |
|res| |
|
399 |
||
1175 | 400 |
res := ByteArray uninitializedNew:count. |
840 | 401 |
|
402 |
1 to:count do:[:i| |
|
403 |
self step. |
|
404 |
res at:i put:(seed bitAnd:16rFF). |
|
405 |
]. |
|
406 |
||
407 |
^ res |
|
408 |
||
409 |
" |
|
410 |
|r| |
|
411 |
r := Random new. |
|
412 |
Transcript showCR:(r nextBytes:20). |
|
413 |
" |
|
414 |
||
415 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
416 |
! |
|
417 |
||
1642 | 418 |
nextCharacters:count |
419 |
"get the next cnt printable characters. |
|
420 |
We answer characters in the ascii range (codepoints 32 - 127)" |
|
421 |
||
422 |
|res| |
|
423 |
||
424 |
res := String uninitializedNew:count. |
|
425 |
||
426 |
1 to:count do:[:i| |
|
427 |
self step. |
|
428 |
res at:i put:(Character value:(seed \\ 95 + 32)). |
|
429 |
]. |
|
430 |
||
431 |
^ res |
|
432 |
||
433 |
" |
|
434 |
Random new nextCharacters:8 |
|
435 |
" |
|
436 |
! |
|
437 |
||
142 | 438 |
nextInteger |
439 |
"return the next integral random number, |
|
440 |
in the range 0 .. 16r3FFFFFFF. |
|
441 |
From Sedgewick's 'Algorithms', based on Lehmer's method" |
|
442 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
443 |
self step. |
142 | 444 |
^ seed |
445 |
||
840 | 446 |
" |
447 |
|r| |
|
142 | 448 |
r := Random new. |
350 | 449 |
Transcript showCR:r nextInteger. |
450 |
Transcript showCR:r nextInteger. |
|
451 |
Transcript showCR:r nextInteger. |
|
452 |
Transcript showCR:r nextInteger. |
|
142 | 453 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
454 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
455 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 456 |
! |
457 |
||
17 | 458 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
459 |
"return an integral random number between start and stop" |
17 | 460 |
|
461 |
|rnd| |
|
462 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
463 |
rnd := self next. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
464 |
rnd := rnd * ((stop - start) asFloat + 1.0). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
465 |
^ rnd truncated + start |
17 | 466 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
467 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
468 |
|r| |
17 | 469 |
r := Random new. |
350 | 470 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
471 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
472 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
473 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 474 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
475 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
476 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
477 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
478 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
479 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
480 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
481 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
482 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
483 |
Transcript showCR:bag contents |
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 |
|
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
486 |
"Modified: / 21.8.1998 / 14:45:04 / cg" |
0 | 487 |
! ! |
488 |
||
65 | 489 |
!Random methodsFor:'blocked methods'! |
0 | 490 |
|
65 | 491 |
contents |
492 |
"blocked from use - contents makes no sense for random generators" |
|
493 |
||
494 |
self shouldNotImplement |
|
495 |
! |
|
496 |
||
497 |
nextPut: value |
|
498 |
"blocked from use - it makes no sense for randoms" |
|
499 |
||
500 |
self shouldNotImplement |
|
501 |
! ! |
|
502 |
||
503 |
!Random methodsFor:'private'! |
|
0 | 504 |
|
505 |
setSeed |
|
842 | 506 |
"set the initial seed value based on the current time. |
507 |
These numbers implement a maximum period generator which passes |
|
508 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
509 |
the product does not overflow 2 raisedTo:29. |
|
510 |
||
511 |
These numbers are carefully choosen, so don't change them, |
|
512 |
unless you know what you are doing!!" |
|
17 | 513 |
|
1212 | 514 |
"Use both time and processId for seed, to make different processes |
515 |
return different Random numbers" |
|
516 |
seed := (Time millisecondClockValue |
|
517 |
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
|
518 |
RandomSalt isNil ifTrue:[ |
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
519 |
RandomSalt := 1. |
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
520 |
] ifFalse:[ |
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
521 |
RandomSalt := RandomSalt + 1. |
973
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
522 |
]. |
ec6bced2d1f5
add salt to seed (in case random generators are created
Claus Gittinger <cg@exept.de>
parents:
947
diff
changeset
|
523 |
seed := seed + RandomSalt. |
842 | 524 |
multiplier := 1597. |
525 |
increment := 51749. |
|
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
526 |
modulus := 244944 " 244957 " . |
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
527 |
|
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
528 |
self step. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
529 |
|
842 | 530 |
"Modified: / 1.4.1997 / 22:44:13 / cg" |
531 |
"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
|
532 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
533 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
534 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
535 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
536 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
537 |
"/ seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
538 |
seed := seed * multiplier + increment \\ modulus |
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 |
"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
|
541 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 542 |
! ! |
65 | 543 |
|
142 | 544 |
!Random methodsFor:'testing'! |
545 |
||
546 |
atEnd |
|
547 |
"instances of Random can always give more numbers" |
|
548 |
||
549 |
^ false |
|
550 |
! |
|
551 |
||
552 |
isReadable |
|
553 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
554 |
! |
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 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
557 |
^ false |
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:38:27 / cg" |
142 | 560 |
! ! |
561 |
||
562 |
!Random class methodsFor:'documentation'! |
|
563 |
||
564 |
version |
|
1841
fe14c3e736ef
remove duplicate inherited method
Stefan Vogel <sv@exept.de>
parents:
1642
diff
changeset
|
565 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.37 2007-01-24 15:27:00 stefan Exp $' |
142 | 566 |
! ! |