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