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