author | Claus Gittinger <cg@exept.de> |
Thu, 24 Mar 2016 13:58:08 +0100 | |
changeset 3776 | d1412e4f5779 |
parent 3750 | 54c47e28047d |
child 3781 | 36ed064deb4e |
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 |
||
3675 | 28 |
"{ NameSpace: Smalltalk }" |
29 |
||
0 | 30 |
Stream subclass:#Random |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
31 |
instanceVariableNames:'seed increment multiplier modulus' |
3134 | 32 |
classVariableNames:'SharedGenerator RandomSalt' |
232 | 33 |
poolDictionaries:'' |
34 |
category:'Magnitude-Numbers' |
|
0 | 35 |
! |
36 |
||
65 | 37 |
!Random class methodsFor:'documentation'! |
0 | 38 |
|
31 | 39 |
copyright |
40 |
" |
|
41 |
====================================================================== |
|
42 |
| |
|
43 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
44 |
| Written by Steve Byrne. |
|
45 |
| |
|
46 |
| This file is part of GNU Smalltalk. |
|
47 |
| |
|
48 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
49 |
| under the terms of the GNU General Public License as published by the Free |
|
50 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
51 |
| |
|
52 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
53 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
54 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
55 |
| details. |
|
56 |
| |
|
57 |
| You should have received a copy of the GNU General Public License along with |
|
58 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
59 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
60 |
| |
|
61 |
====================================================================== |
|
62 |
||
63 |
see notice in (Random>>documentation) |
|
64 |
" |
|
65 |
! |
|
66 |
||
67 |
documentation |
|
68 |
" |
|
3353
0a54e2d850aa
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3192
diff
changeset
|
69 |
Warning: this generator should not be used for cryptographic work |
0a54e2d850aa
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3192
diff
changeset
|
70 |
|
2044 | 71 |
simple random numbers - thanks to Steves GNU Smalltalk |
947 | 72 |
|
842 | 73 |
This implements a linear congruential maximum period random number generator |
74 |
which passes the spectral test for randomness for dimensions 2 3 4 5 6. |
|
75 |
||
2044 | 76 |
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
77 |
======================================================================================= |
|
78 |
DO NOT USE THIS GENERATOR FOR CRYPTOGRAPHY OR OTHER SECURITY RELATED WORK, |
|
79 |
because linear congruential generators are predictable and can be broken easily!! |
|
3776 | 80 |
|
81 |
Smalltalk/X includes a better generator named RandomGenerator, which uses the best |
|
82 |
available generator of the system (either OS-random, /dev/random or as a less dangerous |
|
83 |
fallback, an RC4-based random generator. |
|
84 |
Please use that one. |
|
2044 | 85 |
======================================================================================= |
31 | 86 |
|
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; |
|
312 |
nextPut:ObjectMemory newSpaceUsed. |
|
3383 | 313 |
[ |
3431 | 314 |
hash nextPut:OperatingSystem getCPUCycleCount. |
3383 | 315 |
] on:PrimitiveFailure do:[]. |
3403 | 316 |
|
3414 | 317 |
"/ any other cheap sources of entropy? |
3403 | 318 |
|
319 |
"/ I think there is no problem in that MD5 is not a secure hash algo here - the idea is to shuffle the bits around a bit |
|
320 |
"/ (because the numbers above usually have many high bits in common) |
|
321 |
"/ and then condense the bits into a smaller number. |
|
322 |
"/ Any comment from a crypto guy here - I am willing to change this to some other hash, if that makes a problem |
|
323 |
||
3414 | 324 |
"/ Seeding rnd generators should take some bits from the returned number (i.e. their max. seed size) |
325 |
^ LargeInteger digitBytes:hash hashValue. |
|
3383 | 326 |
|
327 |
" |
|
3403 | 328 |
10 timesRepeat:[Transcript showCR:self randomSeed]. |
3414 | 329 |
10 timesRepeat:[Transcript showCR:(self randomSeed bitAnd:16rFFFF)]. |
3403 | 330 |
self randomSeed bitAnd:16rFFFFFFFF |
3383 | 331 |
" |
332 |
! ! |
|
333 |
||
518 | 334 |
!Random class methodsFor:'testing'! |
335 |
||
793 | 336 |
bucketTest: randy |
337 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
338 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
339 |
indicates something is very wrong. Each run generates different values. |
|
340 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
341 |
go get coffee. |
|
342 |
This is a poor test; see Knuth. |
|
343 |
Some 'OK' runs: |
|
344 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
345 |
1011 987 982 980 982 974 968 1044 976 |
|
346 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
347 |
978 1035 995 988 1038 1009 988 993 976 |
|
348 |
" |
|
349 |
||
350 |
| nbuckets buckets ntrys slot | |
|
351 |
||
352 |
nbuckets := 20. |
|
3192 | 353 |
buckets := Array new: nbuckets withAll:0. |
793 | 354 |
ntrys := 1000. |
355 |
ntrys*nbuckets timesRepeat: [ |
|
356 |
slot := (randy next * nbuckets) floor + 1. |
|
357 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
358 |
Transcript cr. |
|
359 |
1 to: nbuckets do: [ :nb | |
|
360 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
361 |
||
362 |
||
363 |
"Execute this: |
|
3192 | 364 |
self bucketTest: self new |
365 |
self bucketTest: RandomGenerator new |
|
793 | 366 |
" |
367 |
! |
|
368 |
||
518 | 369 |
chiSquareTest |
370 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
371 |
o N = number of samples |
|
372 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
373 |
o Random number generator 'passes' if chisquare value is very close to r |
|
374 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
375 |
||
3192 | 376 |
| aGenerator frequencies n range t | |
377 |
||
378 |
aGenerator := self new. "Seeded differently each time (if seeded at all)" |
|
379 |
range := 100. |
|
380 |
n := 10000. |
|
381 |
frequencies := Array new:range withAll:0. |
|
518 | 382 |
|
3192 | 383 |
1 to: n do: [:i | |
384 |
t := ((aGenerator next) * range) truncated + 1. |
|
385 |
frequencies at:t put: ((frequencies at:t) + 1). |
|
518 | 386 |
]. |
3192 | 387 |
t := frequencies inject:0 into: [:nextValue :eachFreq | |
388 |
nextValue + eachFreq squared |
|
389 |
]. |
|
390 |
^ ((range * t / n) - n) asFloat. |
|
518 | 391 |
|
392 |
" |
|
3192 | 393 |
self chiSquareTest |
394 |
RandomGenerator chiSquareTest |
|
395 |
" |
|
396 |
||
397 |
" |
|
398 |
|fail| |
|
399 |
fail := 0. |
|
400 |
10 timesRepeat:[ |
|
401 |
|testResult| |
|
402 |
testResult := RandomGenerator chiSquareTest. |
|
403 |
(100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1]. |
|
404 |
]. |
|
405 |
fail > 1 ifTrue:[self error:'test failed']. |
|
518 | 406 |
" |
407 |
||
408 |
" |
|
409 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
410 |
The closer to 100, the better. |
|
411 |
" |
|
412 |
! ! |
|
413 |
||
1201 | 414 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 415 |
|
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
416 |
nextInt:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
417 |
"Answer a random integer in the interval [1, anInteger]." |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
418 |
|
3188 | 419 |
(upperBound < 1) ifTrue:[self error:'invalid upper bound']. |
420 |
||
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
421 |
^ self nextIntegerBetween:1 and:upperBound |
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 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
424 |
Random new nextInt:10 |
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 |
! |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
427 |
|
928 | 428 |
nextIntFrom:lowerBound to:upperBound |
429 |
"return a random integer in the given range" |
|
430 |
||
431 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
432 |
||
433 |
" |
|
434 |
Random new nextIntFrom:5 to:10 |
|
435 |
" |
|
3060
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 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
438 |
seed: anInteger |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
439 |
self setSeed: anInteger. |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
440 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
441 |
"Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
928 | 442 |
! ! |
443 |
||
65 | 444 |
!Random methodsFor:'accessing-reading'! |
0 | 445 |
|
142 | 446 |
next |
3675 | 447 |
"return the next random number in the range ]0..1[" |
17 | 448 |
|
3134 | 449 |
self step. |
450 |
^ seed / modulus asFloat |
|
17 | 451 |
|
3134 | 452 |
" |
453 |
|r| |
|
17 | 454 |
r := Random new. |
350 | 455 |
Transcript showCR:r next. |
456 |
Transcript showCR:r next. |
|
457 |
Transcript showCR:r next. |
|
458 |
Transcript showCR:r next. |
|
17 | 459 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
460 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
461 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 462 |
! |
463 |
||
65 | 464 |
nextBetween:start and:stop |
3675 | 465 |
"return a random number in the range ]start..stop[. |
65 | 466 |
claus: the original GNU version has a bug in returning values |
467 |
from the interval [start .. stop+1]" |
|
468 |
||
469 |
|rnd| |
|
470 |
||
471 |
rnd := self next. |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
472 |
rnd := rnd * (stop - start) asFloat. |
65 | 473 |
rnd := rnd + start asFloat. |
474 |
^ rnd |
|
475 |
||
476 |
"|r| |
|
477 |
r := Random new. |
|
350 | 478 |
Transcript showCR:(r nextBetween:1 and:10). |
479 |
Transcript showCR:(r nextBetween:1 and:10). |
|
480 |
Transcript showCR:(r nextBetween:1 and:10). |
|
481 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 482 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
483 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
484 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 485 |
! |
486 |
||
0 | 487 |
nextBoolean |
17 | 488 |
"return true or false by random" |
489 |
||
3134 | 490 |
self step. |
491 |
^ seed < (modulus // 2) |
|
17 | 492 |
|
3134 | 493 |
" |
494 |
|r| |
|
17 | 495 |
r := Random new. |
350 | 496 |
Transcript showCR:r nextBoolean. |
497 |
Transcript showCR:r nextBoolean. |
|
498 |
Transcript showCR:r nextBoolean. |
|
499 |
Transcript showCR:r nextBoolean. |
|
17 | 500 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
501 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
502 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
503 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
504 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
505 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
506 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
507 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
508 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
509 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
510 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
511 |
|
2045 | 512 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 513 |
! |
514 |
||
840 | 515 |
nextByte |
516 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
517 |
||
518 |
self step. |
|
519 |
^ seed bitAnd:16rFF |
|
520 |
||
521 |
" |
|
522 |
|r| |
|
3134 | 523 |
r := self new. |
840 | 524 |
Transcript showCR:r nextByte. |
525 |
Transcript showCR:r nextByte. |
|
526 |
Transcript showCR:r nextByte. |
|
527 |
Transcript showCR:r nextByte. |
|
528 |
" |
|
529 |
||
530 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
531 |
! |
|
532 |
||
533 |
nextBytes:count |
|
978 | 534 |
"return count random bytes (0..16rFF each)" |
840 | 535 |
|
3134 | 536 |
|res cnt "{Class: SmallInteger}"| |
840 | 537 |
|
3134 | 538 |
cnt := count. |
539 |
res := ByteArray uninitializedNew:cnt. |
|
840 | 540 |
|
3134 | 541 |
1 to:cnt do:[:i| |
840 | 542 |
self step. |
543 |
res at:i put:(seed bitAnd:16rFF). |
|
544 |
]. |
|
545 |
||
546 |
^ res |
|
547 |
||
548 |
" |
|
3134 | 549 |
Transcript showCR:(Random new nextBytes:20). |
840 | 550 |
" |
551 |
||
552 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
553 |
! |
|
554 |
||
1642 | 555 |
nextCharacters:count |
556 |
"get the next cnt printable characters. |
|
3750 | 557 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
1642 | 558 |
|
3750 | 559 |
|res cnt "{ Class:SmallInteger }"| |
1642 | 560 |
|
3750 | 561 |
cnt := count. |
562 |
res := String uninitializedNew:cnt. |
|
1642 | 563 |
|
3750 | 564 |
1 to:cnt do:[:i| |
1642 | 565 |
self step. |
3750 | 566 |
res at:i put:(Character value:(seed \\ 94 + 32)). |
1642 | 567 |
]. |
568 |
||
569 |
^ res |
|
570 |
||
571 |
" |
|
572 |
Random new nextCharacters:8 |
|
573 |
" |
|
574 |
! |
|
575 |
||
142 | 576 |
nextInteger |
577 |
"return the next integral random number, |
|
3134 | 578 |
in the range 0 .. modulus (which is less than 16r3FFFFFFF). |
579 |
From Sedgewick's 'Algorithms', based on Lehmer's method. |
|
580 |
||
581 |
Take care, this returns an even number after each odd number!!" |
|
142 | 582 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
583 |
self step. |
142 | 584 |
^ seed |
585 |
||
840 | 586 |
" |
587 |
|r| |
|
142 | 588 |
r := Random new. |
350 | 589 |
Transcript showCR:r nextInteger. |
590 |
Transcript showCR:r nextInteger. |
|
591 |
Transcript showCR:r nextInteger. |
|
592 |
Transcript showCR:r nextInteger. |
|
142 | 593 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
594 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
595 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 596 |
! |
597 |
||
17 | 598 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
599 |
"return an integral random number between start and stop" |
17 | 600 |
|
3026 | 601 |
|rnd range bytesNeeded| |
17 | 602 |
|
3026 | 603 |
range := stop - start + 1. |
3134 | 604 |
|
605 |
range < modulus ifTrue:[ |
|
606 |
"we need the float computation in order to not return alternate even and odd numbers" |
|
607 |
rnd := (self next * range) truncated. |
|
608 |
^ rnd + start. |
|
609 |
] ifFalse:[ |
|
610 |
bytesNeeded := (range highBit + 7) // 8. |
|
611 |
rnd := LargeInteger digitBytes:(self nextBytes:bytesNeeded). |
|
612 |
rnd := rnd bitXor:(seed < (modulus//2) ifTrue:[0] ifFalse:[1]). "do not alternately return even and odd numbers" |
|
613 |
^ start + (rnd \\ range). |
|
614 |
]. |
|
17 | 615 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
616 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
617 |
|r| |
3026 | 618 |
r := self new. |
350 | 619 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
620 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
621 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
622 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 623 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
624 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
625 |
" |
3134 | 626 |
|r| |
627 |
r := self new. |
|
628 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
629 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
630 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
631 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
632 |
" |
|
633 |
||
634 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
635 |
|r bag| |
3026 | 636 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
637 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
638 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
639 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
640 |
]. |
3134 | 641 |
Transcript showCR:bag sortedCounts. |
3026 | 642 |
" |
643 |
||
644 |
" |
|
645 |
|r bag| |
|
646 |
r := self new. |
|
647 |
bag := Bag new. |
|
648 |
1000000 timesRepeat:[ |
|
649 |
bag add:(r nextIntegerBetween:1 and:3). |
|
650 |
]. |
|
3134 | 651 |
Transcript showCR:bag sortedCounts. |
3026 | 652 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
653 |
" |
|
654 |
||
655 |
" |
|
656 |
|r bag| |
|
657 |
r := self new. |
|
658 |
bag := Bag new. |
|
659 |
1000000 timesRepeat:[ |
|
3134 | 660 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 661 |
]. |
3134 | 662 |
Transcript showCR:bag sortedCounts. |
663 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
664 |
" |
|
665 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
666 |
" |
3134 | 667 |
|r bag| |
668 |
r := self new. |
|
669 |
bag := Bag new. |
|
670 |
100000000 timesRepeat:[ |
|
671 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
672 |
]. |
|
673 |
Transcript showCR:bag sortedCounts. |
|
674 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
675 |
" |
|
676 |
||
3675 | 677 |
" |
678 |
|r| |
|
679 |
||
680 |
r := self new. |
|
681 |
100000000 timesRepeat:[ |
|
682 |
self assert:((r nextIntegerBetween:1 and:3) between:1 and:3). |
|
683 |
]. |
|
684 |
" |
|
3750 | 685 |
! |
686 |
||
687 |
nextLettersOrDigits:count |
|
688 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
689 |
||
690 |
|res cnt "{ Class:SmallInteger }"| |
|
691 |
||
692 |
cnt := count. |
|
693 |
res := String uninitializedNew:cnt. |
|
694 |
||
695 |
1 to:cnt do:[:i| |
|
696 |
self step. |
|
697 |
res |
|
698 |
at:i |
|
699 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)). |
|
700 |
]. |
|
701 |
||
702 |
^ res |
|
703 |
||
704 |
" |
|
705 |
Random new nextLettersOrDigits:40 |
|
706 |
" |
|
0 | 707 |
! ! |
708 |
||
65 | 709 |
!Random methodsFor:'blocked methods'! |
0 | 710 |
|
65 | 711 |
contents |
712 |
"blocked from use - contents makes no sense for random generators" |
|
713 |
||
714 |
self shouldNotImplement |
|
715 |
! |
|
716 |
||
717 |
nextPut: value |
|
718 |
"blocked from use - it makes no sense for randoms" |
|
719 |
||
720 |
self shouldNotImplement |
|
721 |
! ! |
|
722 |
||
2459 | 723 |
!Random methodsFor:'initialization'! |
724 |
||
725 |
initialize |
|
726 |
self setSeed |
|
727 |
! ! |
|
728 |
||
65 | 729 |
!Random methodsFor:'private'! |
0 | 730 |
|
2430 | 731 |
addEntropy:entropyBytes |
732 |
"add some entropy - ignored here" |
|
733 |
||
734 |
^ self |
|
735 |
! |
|
736 |
||
2291 | 737 |
seed |
738 |
^ seed |
|
739 |
! |
|
740 |
||
0 | 741 |
setSeed |
3134 | 742 |
"set the initial seed value based on the current time and processId. |
743 |
These numbers implement a maximum period generator which passes |
|
744 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
745 |
the product does not overflow 2 raisedTo:29. |
|
746 |
||
747 |
Use both time and processId for seed, to make different processes |
|
748 |
return different Random numbers" |
|
749 |
||
750 |
|newSeed| |
|
751 |
||
752 |
RandomSalt isNil ifTrue:[ |
|
753 |
RandomSalt := 1. |
|
754 |
] ifFalse:[ |
|
755 |
RandomSalt := RandomSalt + 1. |
|
756 |
]. |
|
757 |
newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId). |
|
758 |
self setSeed:newSeed. |
|
759 |
||
760 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
761 |
! |
|
762 |
||
763 |
setSeed:seedValue |
|
764 |
"set the initial seed and intialite the PRNG parameters. |
|
842 | 765 |
These numbers implement a maximum period generator which passes |
766 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
767 |
the product does not overflow 2 raisedTo:29. |
|
768 |
||
769 |
These numbers are carefully choosen, so don't change them, |
|
770 |
unless you know what you are doing!!" |
|
17 | 771 |
|
3134 | 772 |
modulus := 244944 " 244957 " . |
842 | 773 |
multiplier := 1597. |
774 |
increment := 51749. |
|
3134 | 775 |
seed := seedValue \\ modulus. |
1127
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
776 |
|
2bace63d7bd5
changed initial seed generation, to avoid repeating
Claus Gittinger <cg@exept.de>
parents:
998
diff
changeset
|
777 |
self step. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
778 |
|
1878 | 779 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
780 |
"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
|
781 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
782 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
783 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
784 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
785 |
|
3134 | 786 |
seed := (seed * multiplier + increment) \\ modulus |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
787 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
788 |
"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
|
789 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 790 |
! ! |
65 | 791 |
|
142 | 792 |
!Random methodsFor:'testing'! |
793 |
||
794 |
atEnd |
|
795 |
"instances of Random can always give more numbers" |
|
796 |
||
797 |
^ false |
|
798 |
! |
|
799 |
||
800 |
isReadable |
|
801 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
802 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
803 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
804 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
805 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
806 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
807 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 808 |
! ! |
809 |
||
810 |
!Random class methodsFor:'documentation'! |
|
811 |
||
812 |
version |
|
3675 | 813 |
^ '$Header$' |
2291 | 814 |
! |
815 |
||
816 |
version_CVS |
|
3675 | 817 |
^ '$Header$' |
142 | 818 |
! ! |
3026 | 819 |