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