author | Claus Gittinger <cg@exept.de> |
Fri, 02 Mar 2018 16:15:00 +0100 | |
changeset 4599 | 169bd0f282fb |
parent 4590 | 26321c0bc45c |
child 4600 | ee760be38331 |
permissions | -rw-r--r-- |
4599 | 1 |
"{ Encoding: utf8 }" |
2 |
||
65 | 3 |
" |
4 |
====================================================================== |
|
0 | 5 |
| |
6 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
7 |
| Written by Steve Byrne. |
|
8 |
| |
|
9 |
| This file is part of GNU Smalltalk. |
|
10 |
| |
|
11 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
12 |
| under the terms of the GNU General Public License as published by the Free |
|
13 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
14 |
| |
|
15 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
16 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
17 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
18 |
| details. |
|
19 |
| |
|
20 |
| You should have received a copy of the GNU General Public License along with |
|
21 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
22 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
23 |
| |
|
65 | 24 |
====================================================================== |
0 | 25 |
|
65 | 26 |
see notice in (Random>>documentation) |
0 | 27 |
" |
928 | 28 |
"{ Package: 'stx:libbasic2' }" |
29 |
||
3675 | 30 |
"{ NameSpace: Smalltalk }" |
31 |
||
0 | 32 |
Stream subclass:#Random |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
33 |
instanceVariableNames:'seed increment multiplier modulus' |
3134 | 34 |
classVariableNames:'SharedGenerator RandomSalt' |
232 | 35 |
poolDictionaries:'' |
4590 | 36 |
category:'Magnitude-Numbers-Random' |
0 | 37 |
! |
38 |
||
65 | 39 |
!Random class methodsFor:'documentation'! |
0 | 40 |
|
31 | 41 |
copyright |
42 |
" |
|
43 |
====================================================================== |
|
44 |
| |
|
45 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
46 |
| Written by Steve Byrne. |
|
47 |
| |
|
48 |
| This file is part of GNU Smalltalk. |
|
49 |
| |
|
50 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
51 |
| under the terms of the GNU General Public License as published by the Free |
|
52 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
53 |
| |
|
54 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
55 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
56 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
57 |
| details. |
|
58 |
| |
|
59 |
| You should have received a copy of the GNU General Public License along with |
|
60 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
61 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
62 |
| |
|
63 |
====================================================================== |
|
64 |
||
65 |
see notice in (Random>>documentation) |
|
66 |
" |
|
67 |
! |
|
68 |
||
69 |
documentation |
|
70 |
" |
|
2044 | 71 |
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
72 |
======================================================================================= |
|
73 |
DO NOT USE THIS GENERATOR FOR CRYPTOGRAPHY OR OTHER SECURITY RELATED WORK, |
|
74 |
because linear congruential generators are predictable and can be broken easily!! |
|
3776 | 75 |
|
76 |
Smalltalk/X includes a better generator named RandomGenerator, which uses the best |
|
77 |
available generator of the system (either OS-random, /dev/random or as a less dangerous |
|
4481 | 78 |
fallback, an RC4-based random generator). |
3776 | 79 |
Please use that one. |
2044 | 80 |
======================================================================================= |
31 | 81 |
|
4481 | 82 |
A simple random numbers - thanks to Steves GNU Smalltalk |
83 |
||
84 |
This implements a linear congruential maximum period random number generator |
|
85 |
which passes the spectral test for randomness for dimensions 2 3 4 5 6. |
|
86 |
||
31 | 87 |
Notice: although being included here, |
258 | 88 |
this file is NOT covered by the ST/X license, but by |
89 |
the FSF copyLeft (see copyright method). |
|
31 | 90 |
|
258 | 91 |
You can redistribute it under the terms stated there ... |
92 |
Also, the price you pay for ST/X does not include a charge for |
|
93 |
this file - it has to be considered as a separate piece of |
|
94 |
software, which can be copied and given away without any |
|
95 |
restriction from my (CG) side. |
|
75 | 96 |
|
258 | 97 |
[author:] |
271 | 98 |
Steve Byrne |
258 | 99 |
Claus Gittinger |
947 | 100 |
|
101 |
[see also:] |
|
3394 | 102 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf |
998 | 103 |
RandomTT800 - a new random generator |
104 |
RandomParkMiller - another new random generator |
|
271 | 105 |
" |
106 |
! |
|
107 |
||
108 |
examples |
|
109 |
" |
|
110 |
[exBegin] |
|
111 |
|rnd| |
|
258 | 112 |
|
271 | 113 |
rnd := Random new. |
114 |
10 timesRepeat:[ |
|
350 | 115 |
Transcript showCR:(rnd next) |
271 | 116 |
] |
117 |
[exEnd] |
|
118 |
||
119 |
rolling a dice: |
|
120 |
[exBegin] |
|
121 |
|rnd| |
|
122 |
||
123 |
rnd := Random new. |
|
124 |
10 timesRepeat:[ |
|
350 | 125 |
Transcript showCR:(rnd nextIntegerBetween:1 and:6) |
271 | 126 |
] |
127 |
[exEnd] |
|
31 | 128 |
" |
129 |
! ! |
|
0 | 130 |
|
65 | 131 |
!Random class methodsFor:'instance creation'! |
0 | 132 |
|
133 |
new |
|
17 | 134 |
"return a new random generator" |
135 |
||
2291 | 136 |
^ self basicNew initialize |
842 | 137 |
! |
138 |
||
139 |
random |
|
140 |
"return a new random generator. |
|
141 |
Defined here for compatibility with StreamCipher" |
|
142 |
||
143 |
^ self new |
|
144 |
||
145 |
"Created: / 12.11.1999 / 17:52:08 / stefan" |
|
1128 | 146 |
! |
147 |
||
1878 | 148 |
seed:seedValue |
149 |
"return a new random generator with initial seed" |
|
150 |
||
151 |
^self basicNew setSeed:seedValue |
|
152 |
||
153 |
"Created: / 26-05-2007 / 21:27:18 / cg" |
|
154 |
! |
|
155 |
||
1128 | 156 |
sharedGenerator |
157 |
"return a shared random generator." |
|
158 |
||
3134 | 159 |
SharedGenerator isNil ifTrue:[ |
160 |
SharedGenerator := self new. |
|
1128 | 161 |
]. |
3134 | 162 |
^ SharedGenerator |
2291 | 163 |
! |
164 |
||
165 |
standard |
|
166 |
"return the 'standard' generator" |
|
167 |
||
3134 | 168 |
^ self new |
0 | 169 |
! ! |
170 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
171 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
172 |
|
3171 | 173 |
next |
174 |
"return the next random number in the range 0..1 |
|
175 |
This method behaves like the corresponding instance method, |
|
176 |
but allows generation of random numbers without |
|
177 |
a need for an instance of Random to be kept around. |
|
178 |
This uses a common, shared generator." |
|
179 |
||
180 |
^ self sharedGenerator next. |
|
181 |
||
182 |
" |
|
183 |
Transcript showCR:(Random next). |
|
184 |
Transcript showCR:(Random next). |
|
185 |
Transcript showCR:(Random next). |
|
186 |
Transcript showCR:(Random next). |
|
187 |
" |
|
188 |
! |
|
189 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
190 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
191 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
192 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
193 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
194 |
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
|
195 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
196 |
|
1128 | 197 |
^ self sharedGenerator nextBetween:start and:stop |
561
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 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
200 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
201 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
202 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
203 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
204 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
205 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
206 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
207 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
208 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
209 |
|
851 | 210 |
nextBoolean |
211 |
"return a boolean random. |
|
212 |
This method behaves like the corresponding instance method, |
|
213 |
but allows generation of random numbers without |
|
214 |
a need for an instance of Random to be kept around. |
|
215 |
This uses a common, shared generator." |
|
216 |
||
1128 | 217 |
^ self sharedGenerator nextBoolean. |
851 | 218 |
|
219 |
" |
|
220 |
Transcript showCR:(Random nextBoolean). |
|
221 |
Transcript showCR:(Random nextBoolean). |
|
222 |
Transcript showCR:(Random nextBoolean). |
|
223 |
Transcript showCR:(Random nextBoolean). |
|
224 |
" |
|
225 |
||
226 |
"Created: 21.8.1997 / 18:08:23 / cg" |
|
227 |
! |
|
228 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
229 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
230 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
231 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
232 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
233 |
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
|
234 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
235 |
|
1128 | 236 |
^ self sharedGenerator nextInteger. |
561
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 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
239 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
240 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
241 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
242 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
243 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
244 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
245 |
"Created: 21.8.1997 / 18:08:23 / cg" |
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 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
249 |
"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
|
250 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
251 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
252 |
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
|
253 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
254 |
|
1128 | 255 |
^ self sharedGenerator nextIntegerBetween:start and:stop |
561
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 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
258 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
259 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
260 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
261 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
262 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
263 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
264 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
265 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
3750 | 266 |
! |
267 |
||
268 |
nextLettersOrDigits:count |
|
269 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
270 |
||
271 |
^ self sharedGenerator nextLettersOrDigits:count |
|
272 |
||
273 |
" |
|
274 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
275 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
276 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
277 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
278 |
" |
|
279 |
||
280 |
"Created: 21.8.1997 / 18:07:00 / cg" |
|
281 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
|
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
282 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
283 |
|
3383 | 284 |
!Random class methodsFor:'seeding'! |
285 |
||
286 |
randomSeed |
|
287 |
"return a number useful for seeding. |
|
288 |
This takes the current processor's time, plus the processor's process id, |
|
289 |
plus some value depending on the memory allocation state, |
|
3403 | 290 |
plus a random salt, and shuffles those bits around. |
291 |
The entropy returned should be reasonable enough for a good seed of a good rnd |
|
292 |
generator. However, keep in mind, that it only has a limited number of entropy bits |
|
293 |
(in the order of 32). |
|
3414 | 294 |
But it should be much better than what is commonly used in older |
3403 | 295 |
programs (current time) or even a constant." |
3383 | 296 |
|
3414 | 297 |
|hash| |
298 |
||
3383 | 299 |
RandomSalt isNil ifTrue:[ |
300 |
RandomSalt := 1. |
|
301 |
] ifFalse:[ |
|
302 |
RandomSalt := RandomSalt + 1. |
|
303 |
]. |
|
3431 | 304 |
|
305 |
hash := MD5Stream new. |
|
306 |
hash |
|
307 |
nextPut:RandomSalt; |
|
308 |
nextPut:Time microsecondClockValue; |
|
309 |
nextPut:OperatingSystem getProcessId; |
|
310 |
nextPut:(ObjectMemory addressOf:Object new); |
|
311 |
nextPut:ObjectMemory oldSpaceUsed; |
|
4198 | 312 |
nextPut:ObjectMemory newSpaceUsed; |
313 |
nextPut:OperatingSystem getCPUCycleCount. |
|
3403 | 314 |
|
3414 | 315 |
"/ any other cheap sources of entropy? |
3403 | 316 |
|
4198 | 317 |
"/ I think there is no problem in that MD5 is not a secure hash algo here |
318 |
"/ - the idea is to shuffle the bits around a bit |
|
3403 | 319 |
"/ (because the numbers above usually have many high bits in common) |
320 |
"/ and then condense the bits into a smaller number. |
|
321 |
"/ Any comment from a crypto guy here - I am willing to change this to some other hash, if that makes a problem |
|
322 |
||
3414 | 323 |
"/ Seeding rnd generators should take some bits from the returned number (i.e. their max. seed size) |
324 |
^ LargeInteger digitBytes:hash hashValue. |
|
3383 | 325 |
|
326 |
" |
|
3403 | 327 |
10 timesRepeat:[Transcript showCR:self randomSeed]. |
3414 | 328 |
10 timesRepeat:[Transcript showCR:(self randomSeed bitAnd:16rFFFF)]. |
3403 | 329 |
self randomSeed bitAnd:16rFFFFFFFF |
3383 | 330 |
" |
331 |
! ! |
|
332 |
||
518 | 333 |
!Random class methodsFor:'testing'! |
334 |
||
793 | 335 |
bucketTest: randy |
336 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
337 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
338 |
indicates something is very wrong. Each run generates different values. |
|
339 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
340 |
go get coffee. |
|
341 |
This is a poor test; see Knuth. |
|
342 |
Some 'OK' runs: |
|
343 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
344 |
1011 987 982 980 982 974 968 1044 976 |
|
345 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
346 |
978 1035 995 988 1038 1009 988 993 976 |
|
347 |
" |
|
348 |
||
349 |
| nbuckets buckets ntrys slot | |
|
350 |
||
351 |
nbuckets := 20. |
|
3192 | 352 |
buckets := Array new: nbuckets withAll:0. |
793 | 353 |
ntrys := 1000. |
354 |
ntrys*nbuckets timesRepeat: [ |
|
355 |
slot := (randy next * nbuckets) floor + 1. |
|
356 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
357 |
Transcript cr. |
|
358 |
1 to: nbuckets do: [ :nb | |
|
359 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
360 |
||
361 |
||
362 |
"Execute this: |
|
3192 | 363 |
self bucketTest: self new |
364 |
self bucketTest: RandomGenerator new |
|
793 | 365 |
" |
366 |
! |
|
367 |
||
518 | 368 |
chiSquareTest |
369 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
370 |
o N = number of samples |
|
371 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
372 |
o Random number generator 'passes' if chisquare value is very close to r |
|
373 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
374 |
||
3192 | 375 |
| aGenerator frequencies n range t | |
376 |
||
377 |
aGenerator := self new. "Seeded differently each time (if seeded at all)" |
|
378 |
range := 100. |
|
379 |
n := 10000. |
|
380 |
frequencies := Array new:range withAll:0. |
|
518 | 381 |
|
3192 | 382 |
1 to: n do: [:i | |
383 |
t := ((aGenerator next) * range) truncated + 1. |
|
384 |
frequencies at:t put: ((frequencies at:t) + 1). |
|
518 | 385 |
]. |
3192 | 386 |
t := frequencies inject:0 into: [:nextValue :eachFreq | |
387 |
nextValue + eachFreq squared |
|
388 |
]. |
|
389 |
^ ((range * t / n) - n) asFloat. |
|
518 | 390 |
|
391 |
" |
|
3192 | 392 |
self chiSquareTest |
393 |
RandomGenerator chiSquareTest |
|
394 |
" |
|
395 |
||
396 |
" |
|
397 |
|fail| |
|
398 |
fail := 0. |
|
399 |
10 timesRepeat:[ |
|
400 |
|testResult| |
|
401 |
testResult := RandomGenerator chiSquareTest. |
|
402 |
(100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1]. |
|
403 |
]. |
|
404 |
fail > 1 ifTrue:[self error:'test failed']. |
|
518 | 405 |
" |
406 |
||
407 |
" |
|
408 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
409 |
The closer to 100, the better. |
|
410 |
" |
|
411 |
! ! |
|
412 |
||
1201 | 413 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 414 |
|
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
415 |
nextInt:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
416 |
"Answer a random integer in the interval [1, anInteger]." |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
417 |
|
3188 | 418 |
(upperBound < 1) ifTrue:[self error:'invalid upper bound']. |
419 |
||
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
420 |
^ self nextIntegerBetween:1 and:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
421 |
|
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
422 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
423 |
Random new nextInt:10 |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
424 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
425 |
! |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
426 |
|
928 | 427 |
nextIntFrom:lowerBound to:upperBound |
428 |
"return a random integer in the given range" |
|
429 |
||
430 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
431 |
||
432 |
" |
|
433 |
Random new nextIntFrom:5 to:10 |
|
434 |
" |
|
3060
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
435 |
! |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
436 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
437 |
seed: anInteger |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
438 |
self setSeed: anInteger. |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
439 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
440 |
"Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
928 | 441 |
! ! |
442 |
||
65 | 443 |
!Random methodsFor:'accessing-reading'! |
0 | 444 |
|
142 | 445 |
next |
3675 | 446 |
"return the next random number in the range ]0..1[" |
17 | 447 |
|
3134 | 448 |
self step. |
449 |
^ seed / modulus asFloat |
|
17 | 450 |
|
3134 | 451 |
" |
452 |
|r| |
|
17 | 453 |
r := Random new. |
350 | 454 |
Transcript showCR:r next. |
455 |
Transcript showCR:r next. |
|
456 |
Transcript showCR:r next. |
|
457 |
Transcript showCR:r next. |
|
17 | 458 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
459 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
460 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 461 |
! |
462 |
||
65 | 463 |
nextBetween:start and:stop |
3675 | 464 |
"return a random number in the range ]start..stop[. |
65 | 465 |
claus: the original GNU version has a bug in returning values |
466 |
from the interval [start .. stop+1]" |
|
467 |
||
468 |
|rnd| |
|
469 |
||
470 |
rnd := self next. |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
471 |
rnd := rnd * (stop - start) asFloat. |
65 | 472 |
rnd := rnd + start asFloat. |
473 |
^ rnd |
|
474 |
||
475 |
"|r| |
|
476 |
r := Random new. |
|
350 | 477 |
Transcript showCR:(r nextBetween:1 and:10). |
478 |
Transcript showCR:(r nextBetween:1 and:10). |
|
479 |
Transcript showCR:(r nextBetween:1 and:10). |
|
480 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 481 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
482 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
483 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 484 |
! |
485 |
||
0 | 486 |
nextBoolean |
17 | 487 |
"return true or false by random" |
488 |
||
3134 | 489 |
self step. |
490 |
^ seed < (modulus // 2) |
|
17 | 491 |
|
3134 | 492 |
" |
493 |
|r| |
|
17 | 494 |
r := Random new. |
350 | 495 |
Transcript showCR:r nextBoolean. |
496 |
Transcript showCR:r nextBoolean. |
|
497 |
Transcript showCR:r nextBoolean. |
|
498 |
Transcript showCR:r nextBoolean. |
|
17 | 499 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
500 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
501 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
502 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
503 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
504 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
505 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
506 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
507 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
508 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
509 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
510 |
|
2045 | 511 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 512 |
! |
513 |
||
840 | 514 |
nextByte |
515 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
516 |
||
517 |
self step. |
|
518 |
^ seed bitAnd:16rFF |
|
519 |
||
520 |
" |
|
521 |
|r| |
|
3134 | 522 |
r := self new. |
840 | 523 |
Transcript showCR:r nextByte. |
524 |
Transcript showCR:r nextByte. |
|
525 |
Transcript showCR:r nextByte. |
|
526 |
Transcript showCR:r nextByte. |
|
527 |
" |
|
528 |
||
529 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
530 |
! |
|
531 |
||
532 |
nextBytes:count |
|
978 | 533 |
"return count random bytes (0..16rFF each)" |
840 | 534 |
|
3134 | 535 |
|res cnt "{Class: SmallInteger}"| |
840 | 536 |
|
3134 | 537 |
cnt := count. |
538 |
res := ByteArray uninitializedNew:cnt. |
|
840 | 539 |
|
3134 | 540 |
1 to:cnt do:[:i| |
840 | 541 |
self step. |
542 |
res at:i put:(seed bitAnd:16rFF). |
|
543 |
]. |
|
544 |
||
545 |
^ res |
|
546 |
||
547 |
" |
|
3134 | 548 |
Transcript showCR:(Random new nextBytes:20). |
840 | 549 |
" |
550 |
||
551 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
552 |
! |
|
553 |
||
4599 | 554 |
nextBytesNonZero:count |
555 |
"return count random bytes, none of which is zero (i.e. 1..16rFF each)" |
|
556 |
||
557 |
|res cnt "{Class: SmallInteger}"| |
|
558 |
||
559 |
cnt := count. |
|
560 |
res := ByteArray uninitializedNew:cnt. |
|
561 |
||
562 |
1 to:cnt do:[:i| |
|
563 |
|nextNonZero| |
|
564 |
||
565 |
[ |
|
566 |
self step. |
|
567 |
(nextNonZero := seed bitAnd:16rFF) == 0 |
|
568 |
] whileTrue. |
|
569 |
res at:i put:nextNonZero. |
|
570 |
]. |
|
571 |
||
572 |
^ res |
|
573 |
||
574 |
" |
|
575 |
Transcript showCR:(Random new nextBytesNonZero:20). |
|
576 |
" |
|
577 |
||
578 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
579 |
! |
|
580 |
||
1642 | 581 |
nextCharacters:count |
582 |
"get the next cnt printable characters. |
|
3750 | 583 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
1642 | 584 |
|
3750 | 585 |
|res cnt "{ Class:SmallInteger }"| |
1642 | 586 |
|
3750 | 587 |
cnt := count. |
588 |
res := String uninitializedNew:cnt. |
|
1642 | 589 |
|
3750 | 590 |
1 to:cnt do:[:i| |
1642 | 591 |
self step. |
3750 | 592 |
res at:i put:(Character value:(seed \\ 94 + 32)). |
1642 | 593 |
]. |
594 |
||
595 |
^ res |
|
596 |
||
597 |
" |
|
598 |
Random new nextCharacters:8 |
|
599 |
" |
|
600 |
! |
|
601 |
||
142 | 602 |
nextInteger |
603 |
"return the next integral random number, |
|
3134 | 604 |
in the range 0 .. modulus (which is less than 16r3FFFFFFF). |
605 |
From Sedgewick's 'Algorithms', based on Lehmer's method. |
|
606 |
||
607 |
Take care, this returns an even number after each odd number!!" |
|
142 | 608 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
609 |
self step. |
142 | 610 |
^ seed |
611 |
||
840 | 612 |
" |
613 |
|r| |
|
142 | 614 |
r := Random new. |
350 | 615 |
Transcript showCR:r nextInteger. |
616 |
Transcript showCR:r nextInteger. |
|
617 |
Transcript showCR:r nextInteger. |
|
618 |
Transcript showCR:r nextInteger. |
|
142 | 619 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
620 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
621 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 622 |
! |
623 |
||
17 | 624 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
625 |
"return an integral random number between start and stop" |
17 | 626 |
|
3026 | 627 |
|rnd range bytesNeeded| |
17 | 628 |
|
3026 | 629 |
range := stop - start + 1. |
3134 | 630 |
|
631 |
range < modulus ifTrue:[ |
|
632 |
"we need the float computation in order to not return alternate even and odd numbers" |
|
633 |
rnd := (self next * range) truncated. |
|
634 |
^ rnd + start. |
|
635 |
] ifFalse:[ |
|
636 |
bytesNeeded := (range highBit + 7) // 8. |
|
637 |
rnd := LargeInteger digitBytes:(self nextBytes:bytesNeeded). |
|
638 |
rnd := rnd bitXor:(seed < (modulus//2) ifTrue:[0] ifFalse:[1]). "do not alternately return even and odd numbers" |
|
639 |
^ start + (rnd \\ range). |
|
640 |
]. |
|
17 | 641 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
642 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
643 |
|r| |
3026 | 644 |
r := self new. |
350 | 645 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
646 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
647 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
648 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 649 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
650 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
651 |
" |
3134 | 652 |
|r| |
653 |
r := self new. |
|
654 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
655 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
656 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
657 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
658 |
" |
|
659 |
||
660 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
661 |
|r bag| |
3026 | 662 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
663 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
664 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
665 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
666 |
]. |
3134 | 667 |
Transcript showCR:bag sortedCounts. |
3026 | 668 |
" |
669 |
||
670 |
" |
|
671 |
|r bag| |
|
672 |
r := self new. |
|
673 |
bag := Bag new. |
|
674 |
1000000 timesRepeat:[ |
|
675 |
bag add:(r nextIntegerBetween:1 and:3). |
|
676 |
]. |
|
3134 | 677 |
Transcript showCR:bag sortedCounts. |
3026 | 678 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
679 |
" |
|
680 |
||
681 |
" |
|
682 |
|r bag| |
|
683 |
r := self new. |
|
684 |
bag := Bag new. |
|
685 |
1000000 timesRepeat:[ |
|
3134 | 686 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 687 |
]. |
3134 | 688 |
Transcript showCR:bag sortedCounts. |
689 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
690 |
" |
|
691 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
692 |
" |
3134 | 693 |
|r bag| |
694 |
r := self new. |
|
695 |
bag := Bag new. |
|
696 |
100000000 timesRepeat:[ |
|
697 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
698 |
]. |
|
699 |
Transcript showCR:bag sortedCounts. |
|
700 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
701 |
" |
|
702 |
||
3675 | 703 |
" |
704 |
|r| |
|
705 |
||
706 |
r := self new. |
|
707 |
100000000 timesRepeat:[ |
|
708 |
self assert:((r nextIntegerBetween:1 and:3) between:1 and:3). |
|
709 |
]. |
|
710 |
" |
|
3750 | 711 |
! |
712 |
||
713 |
nextLettersOrDigits:count |
|
714 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
715 |
||
716 |
|res cnt "{ Class:SmallInteger }"| |
|
717 |
||
718 |
cnt := count. |
|
719 |
res := String uninitializedNew:cnt. |
|
720 |
||
721 |
1 to:cnt do:[:i| |
|
722 |
self step. |
|
723 |
res |
|
724 |
at:i |
|
725 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)). |
|
726 |
]. |
|
727 |
||
728 |
^ res |
|
729 |
||
730 |
" |
|
731 |
Random new nextLettersOrDigits:40 |
|
732 |
" |
|
0 | 733 |
! ! |
734 |
||
65 | 735 |
!Random methodsFor:'blocked methods'! |
0 | 736 |
|
65 | 737 |
contents |
738 |
"blocked from use - contents makes no sense for random generators" |
|
739 |
||
740 |
self shouldNotImplement |
|
741 |
! |
|
742 |
||
743 |
nextPut: value |
|
744 |
"blocked from use - it makes no sense for randoms" |
|
745 |
||
746 |
self shouldNotImplement |
|
747 |
! ! |
|
748 |
||
2459 | 749 |
!Random methodsFor:'initialization'! |
750 |
||
751 |
initialize |
|
752 |
self setSeed |
|
753 |
! ! |
|
754 |
||
65 | 755 |
!Random methodsFor:'private'! |
0 | 756 |
|
2430 | 757 |
addEntropy:entropyBytes |
758 |
"add some entropy - ignored here" |
|
759 |
||
760 |
^ self |
|
761 |
! |
|
762 |
||
2291 | 763 |
seed |
764 |
^ seed |
|
765 |
! |
|
766 |
||
0 | 767 |
setSeed |
3134 | 768 |
"set the initial seed value based on the current time and processId. |
769 |
These numbers implement a maximum period generator which passes |
|
770 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
771 |
the product does not overflow 2 raisedTo:29. |
|
772 |
||
773 |
Use both time and processId for seed, to make different processes |
|
774 |
return different Random numbers" |
|
775 |
||
776 |
|newSeed| |
|
777 |
||
778 |
RandomSalt isNil ifTrue:[ |
|
779 |
RandomSalt := 1. |
|
780 |
] ifFalse:[ |
|
781 |
RandomSalt := RandomSalt + 1. |
|
782 |
]. |
|
783 |
newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId). |
|
784 |
self setSeed:newSeed. |
|
785 |
||
786 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
787 |
! |
|
788 |
||
789 |
setSeed:seedValue |
|
790 |
"set the initial seed and intialite the PRNG parameters. |
|
842 | 791 |
These numbers implement a maximum period generator which passes |
792 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
793 |
the product does not overflow 2 raisedTo:29. |
|
794 |
||
795 |
These numbers are carefully choosen, so don't change them, |
|
796 |
unless you know what you are doing!!" |
|
17 | 797 |
|
3134 | 798 |
modulus := 244944 " 244957 " . |
842 | 799 |
multiplier := 1597. |
800 |
increment := 51749. |
|
3134 | 801 |
seed := seedValue \\ modulus. |
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
802 |
|
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
803 |
self step. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
804 |
|
1878 | 805 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
806 |
"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
|
807 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
808 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
809 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
810 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
811 |
|
3134 | 812 |
seed := (seed * multiplier + increment) \\ modulus |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
813 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
814 |
"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
|
815 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 816 |
! ! |
65 | 817 |
|
142 | 818 |
!Random methodsFor:'testing'! |
819 |
||
820 |
atEnd |
|
821 |
"instances of Random can always give more numbers" |
|
822 |
||
823 |
^ false |
|
824 |
! |
|
825 |
||
826 |
isReadable |
|
827 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
828 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
829 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
830 |
isWritable |
3781 | 831 |
"return true, if writing is supported by the receiver. |
832 |
Random Generators never are" |
|
833 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
834 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
835 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
836 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 837 |
! ! |
838 |
||
839 |
!Random class methodsFor:'documentation'! |
|
840 |
||
841 |
version |
|
3675 | 842 |
^ '$Header$' |
2291 | 843 |
! |
844 |
||
845 |
version_CVS |
|
3675 | 846 |
^ '$Header$' |
142 | 847 |
! ! |
3026 | 848 |