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