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