author | Stefan Vogel <sv@exept.de> |
Mon, 03 Mar 2014 15:17:39 +0100 | |
changeset 3192 | fcff2fefb444 |
parent 3188 | e22bc4a307e1 |
child 3353 | 0a54e2d850aa |
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' |
3134 | 30 |
classVariableNames:'SharedGenerator 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 |
||
2291 | 126 |
^ self basicNew initialize |
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 |
||
3134 | 149 |
SharedGenerator isNil ifTrue:[ |
150 |
SharedGenerator := self new. |
|
1128 | 151 |
]. |
3134 | 152 |
^ SharedGenerator |
2291 | 153 |
! |
154 |
||
155 |
standard |
|
156 |
"return the 'standard' generator" |
|
157 |
||
3134 | 158 |
^ self new |
0 | 159 |
! ! |
160 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
161 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
162 |
|
3171 | 163 |
next |
164 |
"return the next random number in the range 0..1 |
|
165 |
This method behaves like the corresponding instance method, |
|
166 |
but allows generation of random numbers without |
|
167 |
a need for an instance of Random to be kept around. |
|
168 |
This uses a common, shared generator." |
|
169 |
||
170 |
^ self sharedGenerator next. |
|
171 |
||
172 |
" |
|
173 |
Transcript showCR:(Random next). |
|
174 |
Transcript showCR:(Random next). |
|
175 |
Transcript showCR:(Random next). |
|
176 |
Transcript showCR:(Random next). |
|
177 |
" |
|
178 |
! |
|
179 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
180 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
181 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
182 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
183 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
184 |
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
|
185 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
186 |
|
1128 | 187 |
^ self sharedGenerator nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
188 |
|
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 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
191 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
192 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
193 |
Transcript showCR:(Random nextBetween:1 and:100). |
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 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
197 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
198 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
199 |
|
851 | 200 |
nextBoolean |
201 |
"return a boolean random. |
|
202 |
This method behaves like the corresponding instance method, |
|
203 |
but allows generation of random numbers without |
|
204 |
a need for an instance of Random to be kept around. |
|
205 |
This uses a common, shared generator." |
|
206 |
||
1128 | 207 |
^ self sharedGenerator nextBoolean. |
851 | 208 |
|
209 |
" |
|
210 |
Transcript showCR:(Random nextBoolean). |
|
211 |
Transcript showCR:(Random nextBoolean). |
|
212 |
Transcript showCR:(Random nextBoolean). |
|
213 |
Transcript showCR:(Random nextBoolean). |
|
214 |
" |
|
215 |
||
216 |
"Created: 21.8.1997 / 18:08:23 / cg" |
|
217 |
! |
|
218 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
219 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
220 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
221 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
222 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
223 |
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
|
224 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
225 |
|
1128 | 226 |
^ self sharedGenerator nextInteger. |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
227 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
228 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
229 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
230 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
231 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
232 |
Transcript showCR:(Random nextInteger). |
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 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
235 |
"Created: 21.8.1997 / 18:08:23 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
236 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
237 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
238 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
239 |
"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
|
240 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
241 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
242 |
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
|
243 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
244 |
|
1128 | 245 |
^ self sharedGenerator nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
246 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
247 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
248 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
249 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
250 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
251 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
252 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
253 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
254 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
255 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
256 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
257 |
|
518 | 258 |
!Random class methodsFor:'testing'! |
259 |
||
793 | 260 |
bucketTest: randy |
261 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
262 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
263 |
indicates something is very wrong. Each run generates different values. |
|
264 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
265 |
go get coffee. |
|
266 |
This is a poor test; see Knuth. |
|
267 |
Some 'OK' runs: |
|
268 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
269 |
1011 987 982 980 982 974 968 1044 976 |
|
270 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
271 |
978 1035 995 988 1038 1009 988 993 976 |
|
272 |
" |
|
273 |
||
274 |
| nbuckets buckets ntrys slot | |
|
275 |
||
276 |
nbuckets := 20. |
|
3192 | 277 |
buckets := Array new: nbuckets withAll:0. |
793 | 278 |
ntrys := 1000. |
279 |
ntrys*nbuckets timesRepeat: [ |
|
280 |
slot := (randy next * nbuckets) floor + 1. |
|
281 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
282 |
Transcript cr. |
|
283 |
1 to: nbuckets do: [ :nb | |
|
284 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
285 |
||
286 |
||
287 |
"Execute this: |
|
3192 | 288 |
self bucketTest: self new |
289 |
self bucketTest: RandomGenerator new |
|
793 | 290 |
" |
291 |
! |
|
292 |
||
518 | 293 |
chiSquareTest |
294 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
295 |
o N = number of samples |
|
296 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
297 |
o Random number generator 'passes' if chisquare value is very close to r |
|
298 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
299 |
||
3192 | 300 |
| aGenerator frequencies n range t | |
301 |
||
302 |
aGenerator := self new. "Seeded differently each time (if seeded at all)" |
|
303 |
range := 100. |
|
304 |
n := 10000. |
|
305 |
frequencies := Array new:range withAll:0. |
|
518 | 306 |
|
3192 | 307 |
1 to: n do: [:i | |
308 |
t := ((aGenerator next) * range) truncated + 1. |
|
309 |
frequencies at:t put: ((frequencies at:t) + 1). |
|
518 | 310 |
]. |
3192 | 311 |
t := frequencies inject:0 into: [:nextValue :eachFreq | |
312 |
nextValue + eachFreq squared |
|
313 |
]. |
|
314 |
^ ((range * t / n) - n) asFloat. |
|
518 | 315 |
|
316 |
" |
|
3192 | 317 |
self chiSquareTest |
318 |
RandomGenerator chiSquareTest |
|
319 |
" |
|
320 |
||
321 |
" |
|
322 |
|fail| |
|
323 |
fail := 0. |
|
324 |
10 timesRepeat:[ |
|
325 |
|testResult| |
|
326 |
testResult := RandomGenerator chiSquareTest. |
|
327 |
(100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1]. |
|
328 |
]. |
|
329 |
fail > 1 ifTrue:[self error:'test failed']. |
|
518 | 330 |
" |
331 |
||
332 |
" |
|
333 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
334 |
The closer to 100, the better. |
|
335 |
" |
|
336 |
! ! |
|
337 |
||
1201 | 338 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 339 |
|
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
340 |
nextInt:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
341 |
"Answer a random integer in the interval [1, anInteger]." |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
342 |
|
3188 | 343 |
(upperBound < 1) ifTrue:[self error:'invalid upper bound']. |
344 |
||
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
345 |
^ self nextIntegerBetween:1 and:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
346 |
|
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
347 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
348 |
Random new nextInt:10 |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
349 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
350 |
! |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
351 |
|
928 | 352 |
nextIntFrom:lowerBound to:upperBound |
353 |
"return a random integer in the given range" |
|
354 |
||
355 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
356 |
||
357 |
" |
|
358 |
Random new nextIntFrom:5 to:10 |
|
359 |
" |
|
3060
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
360 |
! |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
361 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
362 |
seed: anInteger |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
363 |
self setSeed: anInteger. |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
364 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
365 |
"Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
928 | 366 |
! ! |
367 |
||
65 | 368 |
!Random methodsFor:'accessing-reading'! |
0 | 369 |
|
142 | 370 |
next |
371 |
"return the next random number in the range 0..1" |
|
17 | 372 |
|
3134 | 373 |
self step. |
374 |
^ seed / modulus asFloat |
|
17 | 375 |
|
3134 | 376 |
" |
377 |
|r| |
|
17 | 378 |
r := Random new. |
350 | 379 |
Transcript showCR:r next. |
380 |
Transcript showCR:r next. |
|
381 |
Transcript showCR:r next. |
|
382 |
Transcript showCR:r next. |
|
17 | 383 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
384 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
385 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 386 |
! |
387 |
||
65 | 388 |
nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
389 |
"return a random number between start and stop. |
65 | 390 |
claus: the original GNU version has a bug in returning values |
391 |
from the interval [start .. stop+1]" |
|
392 |
||
393 |
|rnd| |
|
394 |
||
395 |
rnd := self next. |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
396 |
rnd := rnd * (stop - start) asFloat. |
65 | 397 |
rnd := rnd + start asFloat. |
398 |
^ rnd |
|
399 |
||
400 |
"|r| |
|
401 |
r := Random new. |
|
350 | 402 |
Transcript showCR:(r nextBetween:1 and:10). |
403 |
Transcript showCR:(r nextBetween:1 and:10). |
|
404 |
Transcript showCR:(r nextBetween:1 and:10). |
|
405 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 406 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
407 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
408 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 409 |
! |
410 |
||
0 | 411 |
nextBoolean |
17 | 412 |
"return true or false by random" |
413 |
||
3134 | 414 |
self step. |
415 |
^ seed < (modulus // 2) |
|
17 | 416 |
|
3134 | 417 |
" |
418 |
|r| |
|
17 | 419 |
r := Random new. |
350 | 420 |
Transcript showCR:r nextBoolean. |
421 |
Transcript showCR:r nextBoolean. |
|
422 |
Transcript showCR:r nextBoolean. |
|
423 |
Transcript showCR:r nextBoolean. |
|
17 | 424 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
425 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
426 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
427 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
428 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
429 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
430 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
431 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
432 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
433 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
434 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
435 |
|
2045 | 436 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 437 |
! |
438 |
||
840 | 439 |
nextByte |
440 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
441 |
||
442 |
self step. |
|
443 |
^ seed bitAnd:16rFF |
|
444 |
||
445 |
" |
|
446 |
|r| |
|
3134 | 447 |
r := self new. |
840 | 448 |
Transcript showCR:r nextByte. |
449 |
Transcript showCR:r nextByte. |
|
450 |
Transcript showCR:r nextByte. |
|
451 |
Transcript showCR:r nextByte. |
|
452 |
" |
|
453 |
||
454 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
455 |
! |
|
456 |
||
457 |
nextBytes:count |
|
978 | 458 |
"return count random bytes (0..16rFF each)" |
840 | 459 |
|
3134 | 460 |
|res cnt "{Class: SmallInteger}"| |
840 | 461 |
|
3134 | 462 |
cnt := count. |
463 |
res := ByteArray uninitializedNew:cnt. |
|
840 | 464 |
|
3134 | 465 |
1 to:cnt do:[:i| |
840 | 466 |
self step. |
467 |
res at:i put:(seed bitAnd:16rFF). |
|
468 |
]. |
|
469 |
||
470 |
^ res |
|
471 |
||
472 |
" |
|
3134 | 473 |
Transcript showCR:(Random new nextBytes:20). |
840 | 474 |
" |
475 |
||
476 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
477 |
! |
|
478 |
||
1642 | 479 |
nextCharacters:count |
480 |
"get the next cnt printable characters. |
|
481 |
We answer characters in the ascii range (codepoints 32 - 127)" |
|
482 |
||
483 |
|res| |
|
484 |
||
485 |
res := String uninitializedNew:count. |
|
486 |
||
487 |
1 to:count do:[:i| |
|
488 |
self step. |
|
489 |
res at:i put:(Character value:(seed \\ 95 + 32)). |
|
490 |
]. |
|
491 |
||
492 |
^ res |
|
493 |
||
494 |
" |
|
495 |
Random new nextCharacters:8 |
|
496 |
" |
|
497 |
! |
|
498 |
||
142 | 499 |
nextInteger |
500 |
"return the next integral random number, |
|
3134 | 501 |
in the range 0 .. modulus (which is less than 16r3FFFFFFF). |
502 |
From Sedgewick's 'Algorithms', based on Lehmer's method. |
|
503 |
||
504 |
Take care, this returns an even number after each odd number!!" |
|
142 | 505 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
506 |
self step. |
142 | 507 |
^ seed |
508 |
||
840 | 509 |
" |
510 |
|r| |
|
142 | 511 |
r := Random new. |
350 | 512 |
Transcript showCR:r nextInteger. |
513 |
Transcript showCR:r nextInteger. |
|
514 |
Transcript showCR:r nextInteger. |
|
515 |
Transcript showCR:r nextInteger. |
|
142 | 516 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
517 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
518 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 519 |
! |
520 |
||
17 | 521 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
522 |
"return an integral random number between start and stop" |
17 | 523 |
|
3026 | 524 |
|rnd range bytesNeeded| |
17 | 525 |
|
3026 | 526 |
range := stop - start + 1. |
3134 | 527 |
|
528 |
range < modulus ifTrue:[ |
|
529 |
"we need the float computation in order to not return alternate even and odd numbers" |
|
530 |
rnd := (self next * range) truncated. |
|
531 |
^ rnd + start. |
|
532 |
] ifFalse:[ |
|
533 |
bytesNeeded := (range highBit + 7) // 8. |
|
534 |
rnd := LargeInteger digitBytes:(self nextBytes:bytesNeeded). |
|
535 |
rnd := rnd bitXor:(seed < (modulus//2) ifTrue:[0] ifFalse:[1]). "do not alternately return even and odd numbers" |
|
536 |
^ start + (rnd \\ range). |
|
537 |
]. |
|
17 | 538 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
539 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
540 |
|r| |
3026 | 541 |
r := self new. |
350 | 542 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
543 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
544 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
545 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 546 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
547 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
548 |
" |
3134 | 549 |
|r| |
550 |
r := self new. |
|
551 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
552 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
553 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
554 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
555 |
" |
|
556 |
||
557 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
558 |
|r bag| |
3026 | 559 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
560 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
561 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
562 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
563 |
]. |
3134 | 564 |
Transcript showCR:bag sortedCounts. |
3026 | 565 |
" |
566 |
||
567 |
" |
|
568 |
|r bag| |
|
569 |
r := self new. |
|
570 |
bag := Bag new. |
|
571 |
1000000 timesRepeat:[ |
|
572 |
bag add:(r nextIntegerBetween:1 and:3). |
|
573 |
]. |
|
3134 | 574 |
Transcript showCR:bag sortedCounts. |
3026 | 575 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
576 |
" |
|
577 |
||
578 |
" |
|
579 |
|r bag| |
|
580 |
r := self new. |
|
581 |
bag := Bag new. |
|
582 |
1000000 timesRepeat:[ |
|
3134 | 583 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 584 |
]. |
3134 | 585 |
Transcript showCR:bag sortedCounts. |
586 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
587 |
" |
|
588 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
589 |
" |
3134 | 590 |
|r bag| |
591 |
r := self new. |
|
592 |
bag := Bag new. |
|
593 |
100000000 timesRepeat:[ |
|
594 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
595 |
]. |
|
596 |
Transcript showCR:bag sortedCounts. |
|
597 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
598 |
" |
|
599 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
600 |
|
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
601 |
"Modified: / 21.8.1998 / 14:45:04 / cg" |
0 | 602 |
! ! |
603 |
||
65 | 604 |
!Random methodsFor:'blocked methods'! |
0 | 605 |
|
65 | 606 |
contents |
607 |
"blocked from use - contents makes no sense for random generators" |
|
608 |
||
609 |
self shouldNotImplement |
|
610 |
! |
|
611 |
||
612 |
nextPut: value |
|
613 |
"blocked from use - it makes no sense for randoms" |
|
614 |
||
615 |
self shouldNotImplement |
|
616 |
! ! |
|
617 |
||
2459 | 618 |
!Random methodsFor:'initialization'! |
619 |
||
620 |
initialize |
|
621 |
self setSeed |
|
622 |
! ! |
|
623 |
||
65 | 624 |
!Random methodsFor:'private'! |
0 | 625 |
|
2430 | 626 |
addEntropy:entropyBytes |
627 |
"add some entropy - ignored here" |
|
628 |
||
629 |
^ self |
|
630 |
! |
|
631 |
||
2291 | 632 |
seed |
633 |
^ seed |
|
634 |
! |
|
635 |
||
0 | 636 |
setSeed |
3134 | 637 |
"set the initial seed value based on the current time and processId. |
638 |
These numbers implement a maximum period generator which passes |
|
639 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
640 |
the product does not overflow 2 raisedTo:29. |
|
641 |
||
642 |
Use both time and processId for seed, to make different processes |
|
643 |
return different Random numbers" |
|
644 |
||
645 |
|newSeed| |
|
646 |
||
647 |
RandomSalt isNil ifTrue:[ |
|
648 |
RandomSalt := 1. |
|
649 |
] ifFalse:[ |
|
650 |
RandomSalt := RandomSalt + 1. |
|
651 |
]. |
|
652 |
newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId). |
|
653 |
self setSeed:newSeed. |
|
654 |
||
655 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
656 |
! |
|
657 |
||
658 |
setSeed:seedValue |
|
659 |
"set the initial seed and intialite the PRNG parameters. |
|
842 | 660 |
These numbers implement a maximum period generator which passes |
661 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
662 |
the product does not overflow 2 raisedTo:29. |
|
663 |
||
664 |
These numbers are carefully choosen, so don't change them, |
|
665 |
unless you know what you are doing!!" |
|
17 | 666 |
|
3134 | 667 |
modulus := 244944 " 244957 " . |
842 | 668 |
multiplier := 1597. |
669 |
increment := 51749. |
|
3134 | 670 |
seed := seedValue \\ modulus. |
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
671 |
|
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
672 |
self step. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
673 |
|
1878 | 674 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
675 |
"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
|
676 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
677 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
678 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
679 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
680 |
|
3134 | 681 |
seed := (seed * multiplier + increment) \\ modulus |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
682 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
683 |
"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
|
684 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 685 |
! ! |
65 | 686 |
|
142 | 687 |
!Random methodsFor:'testing'! |
688 |
||
689 |
atEnd |
|
690 |
"instances of Random can always give more numbers" |
|
691 |
||
692 |
^ false |
|
693 |
! |
|
694 |
||
695 |
isReadable |
|
696 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
697 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
698 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
699 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
700 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
701 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
702 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 703 |
! ! |
704 |
||
705 |
!Random class methodsFor:'documentation'! |
|
706 |
||
707 |
version |
|
3192 | 708 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $' |
2291 | 709 |
! |
710 |
||
711 |
version_CVS |
|
3192 | 712 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $' |
142 | 713 |
! ! |
3026 | 714 |