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