author | Claus Gittinger <cg@exept.de> |
Sat, 07 Sep 2019 16:08:03 +0200 | |
changeset 5201 | f2906f8a29d3 |
parent 5069 | c96c32f49875 |
child 5214 | c18e2ca15a2c |
permissions | -rw-r--r-- |
65 | 1 |
" |
4600 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
3 |
All Rights Reserved |
|
0 | 4 |
|
4600 | 5 |
This software is furnished under a license and may be used |
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
0 | 11 |
" |
928 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3675 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
0 | 16 |
Stream subclass:#Random |
4600 | 17 |
instanceVariableNames:'seed' |
18 |
classVariableNames:'RandomSalt SharedGenerator' |
|
232 | 19 |
poolDictionaries:'' |
4590 | 20 |
category:'Magnitude-Numbers-Random' |
0 | 21 |
! |
22 |
||
65 | 23 |
!Random class methodsFor:'documentation'! |
0 | 24 |
|
31 | 25 |
copyright |
26 |
" |
|
4600 | 27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
28 |
All Rights Reserved |
|
31 | 29 |
|
4600 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
31 | 36 |
" |
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
4600 | 41 |
A facade to the actual RandomGenerator. |
42 |
In previous versions, Random was a very bad fallback random generator, |
|
43 |
with an included warning, to not use it. |
|
4481 | 44 |
|
4600 | 45 |
Now this old generator was renamed to RandomGNUSmalltalk, |
46 |
and the default generator used is the one provided by the much |
|
47 |
better RandomGenerator class. |
|
4481 | 48 |
|
4600 | 49 |
Random new next:10 |
75 | 50 |
|
258 | 51 |
[author:] |
52 |
Claus Gittinger |
|
947 | 53 |
|
54 |
[see also:] |
|
3394 | 55 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf |
998 | 56 |
RandomTT800 - a new random generator |
57 |
RandomParkMiller - another new random generator |
|
271 | 58 |
" |
59 |
! |
|
60 |
||
61 |
examples |
|
62 |
" |
|
63 |
[exBegin] |
|
64 |
|rnd| |
|
258 | 65 |
|
271 | 66 |
rnd := Random new. |
67 |
10 timesRepeat:[ |
|
350 | 68 |
Transcript showCR:(rnd next) |
271 | 69 |
] |
70 |
[exEnd] |
|
71 |
||
72 |
rolling a dice: |
|
73 |
[exBegin] |
|
74 |
|rnd| |
|
75 |
||
76 |
rnd := Random new. |
|
77 |
10 timesRepeat:[ |
|
350 | 78 |
Transcript showCR:(rnd nextIntegerBetween:1 and:6) |
271 | 79 |
] |
80 |
[exEnd] |
|
31 | 81 |
" |
82 |
! ! |
|
0 | 83 |
|
65 | 84 |
!Random class methodsFor:'instance creation'! |
0 | 85 |
|
86 |
new |
|
4600 | 87 |
"return a new random generator. |
88 |
Here, this instance creation message is forwarded to the standardGenerator" |
|
17 | 89 |
|
4612 | 90 |
self == Random ifFalse:[^ super new]. |
4600 | 91 |
^ self standard |
92 |
||
93 |
" |
|
94 |
Random new |
|
95 |
" |
|
842 | 96 |
! |
97 |
||
98 |
random |
|
99 |
"return a new random generator. |
|
100 |
Defined here for compatibility with StreamCipher" |
|
101 |
||
4600 | 102 |
^ self standard |
842 | 103 |
|
104 |
"Created: / 12.11.1999 / 17:52:08 / stefan" |
|
1128 | 105 |
! |
106 |
||
1878 | 107 |
seed:seedValue |
4600 | 108 |
"return a new random generator with initial seed. |
109 |
Here, this instance creation message is forwarded to the standardGenerator" |
|
1878 | 110 |
|
4613 | 111 |
self == Random ifFalse:[^ self basicNew seed:seedValue]. |
4600 | 112 |
^ self standardGeneratorClass basicNew setSeed:seedValue |
1878 | 113 |
|
114 |
"Created: / 26-05-2007 / 21:27:18 / cg" |
|
115 |
! |
|
116 |
||
1128 | 117 |
sharedGenerator |
118 |
"return a shared random generator." |
|
119 |
||
3134 | 120 |
SharedGenerator isNil ifTrue:[ |
4600 | 121 |
SharedGenerator := self standard. |
1128 | 122 |
]. |
3134 | 123 |
^ SharedGenerator |
2291 | 124 |
! |
125 |
||
126 |
standard |
|
127 |
"return the 'standard' generator" |
|
128 |
||
4600 | 129 |
^ self standardGeneratorClass new |
130 |
! |
|
131 |
||
132 |
standardGeneratorClass |
|
133 |
"return the class used for the 'standard' generator" |
|
134 |
||
135 |
^ RandomGenerator |
|
0 | 136 |
! ! |
137 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
138 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
139 |
|
3171 | 140 |
next |
141 |
"return the next random number in the range 0..1 |
|
142 |
This method behaves like the corresponding instance method, |
|
143 |
but allows generation of random numbers without |
|
144 |
a need for an instance of Random to be kept around. |
|
145 |
This uses a common, shared generator." |
|
146 |
||
147 |
^ self sharedGenerator next. |
|
148 |
||
149 |
" |
|
150 |
Transcript showCR:(Random next). |
|
151 |
Transcript showCR:(Random next). |
|
152 |
Transcript showCR:(Random next). |
|
153 |
Transcript showCR:(Random next). |
|
154 |
" |
|
155 |
! |
|
156 |
||
5069 | 157 |
next:n |
158 |
"return the next n random numbers in the range 0..1 |
|
159 |
This method behaves like the corresponding instance method, |
|
160 |
but allows generation of random numbers without |
|
161 |
a need for an instance of Random to be kept around. |
|
162 |
This uses a common, shared generator." |
|
163 |
||
164 |
^ self sharedGenerator next:n. |
|
165 |
||
166 |
" |
|
167 |
Transcript showCR:(Random next:10). |
|
168 |
" |
|
169 |
! |
|
170 |
||
171 |
next:n between:start and:stop |
|
172 |
"return n random numbers between start and stop. |
|
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:n between:start and:stop |
|
179 |
||
180 |
" |
|
181 |
Transcript showCR:(Random next:10 between:1 and:100). |
|
182 |
" |
|
183 |
||
184 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
|
185 |
"Created: 21.8.1997 / 18:09:36 / cg" |
|
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 |
|
4600 | 446 |
self subclassResponsibility. |
17 | 447 |
|
3134 | 448 |
" |
449 |
|r| |
|
17 | 450 |
r := Random new. |
350 | 451 |
Transcript showCR:r next. |
452 |
Transcript showCR:r next. |
|
453 |
Transcript showCR:r next. |
|
454 |
Transcript showCR:r next. |
|
17 | 455 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
456 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
457 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 458 |
! |
459 |
||
4640 | 460 |
next:count |
461 |
"return the next count random numbers in the range ]0..1[" |
|
462 |
||
463 |
|answerStream |
|
464 |
cnt "{ Class: SmallInteger }" | |
|
465 |
||
466 |
cnt := count. |
|
467 |
answerStream := self contentsSpecies writeStream:cnt. |
|
468 |
1 to:cnt do:[:index | |next| |
|
469 |
next := self next. |
|
470 |
answerStream nextPut:next. |
|
471 |
]. |
|
472 |
^ answerStream contents |
|
473 |
||
474 |
" |
|
475 |
(RandomGenerator new) next:10 |
|
476 |
" |
|
477 |
! |
|
478 |
||
5069 | 479 |
next:count between:min and:max |
480 |
"return the next count random numbers in min..max" |
|
481 |
||
482 |
|answerStream |
|
483 |
cnt "{ Class: SmallInteger }" | |
|
484 |
||
485 |
cnt := count. |
|
486 |
answerStream := self contentsSpecies writeStream:cnt. |
|
487 |
1 to:cnt do:[:index | |next| |
|
488 |
next := self nextBetween:min and:max. |
|
489 |
answerStream nextPut:next. |
|
490 |
]. |
|
491 |
^ answerStream contents |
|
492 |
||
493 |
" |
|
494 |
(RandomGenerator new) next:10 between:0 and:100 |
|
495 |
" |
|
496 |
! |
|
497 |
||
4640 | 498 |
next:count integerBetween:min and:max |
499 |
"return the next count random integers in min..max" |
|
500 |
||
501 |
|answerStream |
|
502 |
cnt "{ Class: SmallInteger }" | |
|
503 |
||
504 |
cnt := count. |
|
505 |
answerStream := self contentsSpecies writeStream:cnt. |
|
506 |
1 to:cnt do:[:index | |next| |
|
507 |
next := self nextIntegerBetween:min and:max. |
|
508 |
answerStream nextPut:next. |
|
509 |
]. |
|
510 |
^ answerStream contents |
|
511 |
||
512 |
" |
|
513 |
(RandomGenerator new) next:100 integerBetween:0 and:100 |
|
514 |
" |
|
515 |
! |
|
516 |
||
65 | 517 |
nextBetween:start and:stop |
3675 | 518 |
"return a random number in the range ]start..stop[. |
65 | 519 |
claus: the original GNU version has a bug in returning values |
520 |
from the interval [start .. stop+1]" |
|
521 |
||
4627 | 522 |
|rndFloat rnd| |
65 | 523 |
|
4627 | 524 |
rndFloat := self next. |
525 |
rnd := (rndFloat * (stop - start) asFloat) + start asFloat. |
|
65 | 526 |
^ rnd |
527 |
||
4614 | 528 |
" |
529 |
|r| |
|
65 | 530 |
r := Random new. |
350 | 531 |
Transcript showCR:(r nextBetween:1 and:10). |
532 |
Transcript showCR:(r nextBetween:1 and:10). |
|
533 |
Transcript showCR:(r nextBetween:1 and:10). |
|
534 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 535 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
536 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
537 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 538 |
! |
539 |
||
0 | 540 |
nextBoolean |
17 | 541 |
"return true or false by random" |
542 |
||
4614 | 543 |
^ self nextInteger bitAnd:16r10. |
17 | 544 |
|
3134 | 545 |
" |
546 |
|r| |
|
17 | 547 |
r := Random new. |
350 | 548 |
Transcript showCR:r nextBoolean. |
549 |
Transcript showCR:r nextBoolean. |
|
550 |
Transcript showCR:r nextBoolean. |
|
551 |
Transcript showCR:r nextBoolean. |
|
17 | 552 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
553 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
554 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
555 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
556 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
557 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
558 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
559 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
560 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
561 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
562 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
563 |
|
2045 | 564 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 565 |
! |
566 |
||
840 | 567 |
nextByte |
568 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
569 |
||
4614 | 570 |
^ self nextInteger bitAnd:16rFF |
840 | 571 |
|
572 |
" |
|
573 |
|r| |
|
3134 | 574 |
r := self new. |
840 | 575 |
Transcript showCR:r nextByte. |
576 |
Transcript showCR:r nextByte. |
|
577 |
Transcript showCR:r nextByte. |
|
578 |
Transcript showCR:r nextByte. |
|
579 |
" |
|
580 |
||
581 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
582 |
! |
|
583 |
||
584 |
nextBytes:count |
|
978 | 585 |
"return count random bytes (0..16rFF each)" |
840 | 586 |
|
4600 | 587 |
^ (1 to:count) collect:[:i | self nextByte] as:ByteArray |
840 | 588 |
|
589 |
" |
|
4600 | 590 |
Transcript showCR:(RandomGenerator new nextBytes:20). |
840 | 591 |
" |
592 |
||
593 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
594 |
! |
|
595 |
||
4599 | 596 |
nextBytesNonZero:count |
597 |
"return count random bytes, none of which is zero (i.e. 1..16rFF each)" |
|
598 |
||
599 |
|res cnt "{Class: SmallInteger}"| |
|
600 |
||
601 |
cnt := count. |
|
602 |
res := ByteArray uninitializedNew:cnt. |
|
603 |
||
604 |
1 to:cnt do:[:i| |
|
605 |
|nextNonZero| |
|
606 |
||
607 |
[ |
|
4611 | 608 |
(nextNonZero := self nextByte) == 0 |
4599 | 609 |
] whileTrue. |
610 |
res at:i put:nextNonZero. |
|
611 |
]. |
|
612 |
||
613 |
^ res |
|
614 |
||
615 |
" |
|
616 |
Transcript showCR:(Random new nextBytesNonZero:20). |
|
617 |
" |
|
618 |
||
619 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
620 |
! |
|
621 |
||
1642 | 622 |
nextCharacters:count |
623 |
"get the next cnt printable characters. |
|
3750 | 624 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
1642 | 625 |
|
4614 | 626 |
^ (1 to:count) collect:[:i | Character value:(self nextInteger \\ 94 + 32) ] as:String |
1642 | 627 |
|
628 |
" |
|
4600 | 629 |
RandomGenerator new nextCharacters:8 |
1642 | 630 |
" |
631 |
! |
|
632 |
||
142 | 633 |
nextInteger |
634 |
"return the next integral random number, |
|
4600 | 635 |
in the range 0 .. self maxInteger" |
3134 | 636 |
|
4600 | 637 |
self subclassResponsibility |
142 | 638 |
|
840 | 639 |
" |
640 |
|r| |
|
142 | 641 |
r := Random new. |
350 | 642 |
Transcript showCR:r nextInteger. |
643 |
Transcript showCR:r nextInteger. |
|
644 |
Transcript showCR:r nextInteger. |
|
645 |
Transcript showCR:r nextInteger. |
|
142 | 646 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
647 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
648 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 649 |
! |
650 |
||
17 | 651 |
nextIntegerBetween:start and:stop |
4600 | 652 |
"return an integral random number in [start..stop] (inclusive)" |
17 | 653 |
|
3026 | 654 |
|rnd range bytesNeeded| |
17 | 655 |
|
3026 | 656 |
range := stop - start + 1. |
4600 | 657 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
658 |
bytesNeeded := (range highBit + 15) // 8. |
|
659 |
bytesNeeded < 4 ifTrue:[ |
|
660 |
rnd := (((self nextByte bitShift:8) + self nextByte) bitShift:8) + self nextByte. |
|
3134 | 661 |
] ifFalse:[ |
4600 | 662 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
3134 | 663 |
]. |
5201 | 664 |
rnd := rnd \\ range. |
4600 | 665 |
^ rnd + start |
666 |
||
667 |
"shows pair-combination counts |
|
668 |
||
669 |
|r v prev this counts| |
|
670 |
||
671 |
v := TextView new . |
|
672 |
v extent:500@250. |
|
673 |
v list:#('xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
674 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
675 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
676 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
677 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
678 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
679 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
680 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
681 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
682 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'). |
|
683 |
v openAndWait. |
|
684 |
counts := (1 to:10) collect:[:row | Array new:10 withAll:0]. |
|
685 |
||
686 |
r := self new. |
|
687 |
prev := r nextIntegerBetween:1 and:10. |
|
688 |
10000 timesRepeat:[ |
|
689 |
100 timesRepeat:[ |
|
690 |
this := r nextIntegerBetween:1 and:10. |
|
691 |
(counts at:this) at:prev put:((counts at:this) at:prev)+1. |
|
692 |
]. |
|
693 |
v list:(counts collect:[:row | |
|
694 |
'%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d'printf:row ]). |
|
695 |
prev := this. |
|
696 |
]. |
|
697 |
" |
|
17 | 698 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
699 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
700 |
|r| |
3026 | 701 |
r := self new. |
350 | 702 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
703 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
704 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
705 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 706 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
707 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
708 |
" |
3134 | 709 |
|r| |
710 |
r := self new. |
|
711 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
712 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
713 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
714 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
715 |
" |
|
716 |
||
717 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
718 |
|r bag| |
3026 | 719 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
720 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
721 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
722 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
723 |
]. |
3134 | 724 |
Transcript showCR:bag sortedCounts. |
3026 | 725 |
" |
726 |
||
727 |
" |
|
728 |
|r bag| |
|
729 |
r := self new. |
|
730 |
bag := Bag new. |
|
731 |
1000000 timesRepeat:[ |
|
732 |
bag add:(r nextIntegerBetween:1 and:3). |
|
733 |
]. |
|
3134 | 734 |
Transcript showCR:bag sortedCounts. |
3026 | 735 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
736 |
" |
|
737 |
||
738 |
" |
|
739 |
|r bag| |
|
740 |
r := self new. |
|
741 |
bag := Bag new. |
|
742 |
1000000 timesRepeat:[ |
|
3134 | 743 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 744 |
]. |
3134 | 745 |
Transcript showCR:bag sortedCounts. |
746 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
747 |
" |
|
748 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
749 |
" |
3134 | 750 |
|r bag| |
751 |
r := self new. |
|
752 |
bag := Bag new. |
|
753 |
100000000 timesRepeat:[ |
|
754 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
755 |
]. |
|
756 |
Transcript showCR:bag sortedCounts. |
|
757 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
758 |
" |
|
759 |
||
3675 | 760 |
" |
761 |
|r| |
|
762 |
||
763 |
r := self new. |
|
764 |
100000000 timesRepeat:[ |
|
765 |
self assert:((r nextIntegerBetween:1 and:3) between:1 and:3). |
|
766 |
]. |
|
767 |
" |
|
3750 | 768 |
! |
769 |
||
770 |
nextLettersOrDigits:count |
|
4600 | 771 |
"get a random string consisting of count printable letters or digits [0-9A-Za-z]." |
3750 | 772 |
|
773 |
|res cnt "{ Class:SmallInteger }"| |
|
774 |
||
775 |
cnt := count. |
|
776 |
res := String uninitializedNew:cnt. |
|
777 |
||
778 |
1 to:cnt do:[:i| |
|
779 |
self step. |
|
780 |
res |
|
781 |
at:i |
|
782 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)). |
|
783 |
]. |
|
784 |
||
785 |
^ res |
|
786 |
||
787 |
" |
|
4600 | 788 |
Random new nextLettersOrDigits:40 |
3750 | 789 |
" |
0 | 790 |
! ! |
791 |
||
65 | 792 |
!Random methodsFor:'blocked methods'! |
0 | 793 |
|
65 | 794 |
contents |
795 |
"blocked from use - contents makes no sense for random generators" |
|
796 |
||
797 |
self shouldNotImplement |
|
798 |
! |
|
799 |
||
800 |
nextPut: value |
|
801 |
"blocked from use - it makes no sense for randoms" |
|
802 |
||
803 |
self shouldNotImplement |
|
804 |
! ! |
|
805 |
||
2459 | 806 |
!Random methodsFor:'initialization'! |
807 |
||
808 |
initialize |
|
809 |
self setSeed |
|
810 |
! ! |
|
811 |
||
65 | 812 |
!Random methodsFor:'private'! |
0 | 813 |
|
2430 | 814 |
addEntropy:entropyBytes |
815 |
"add some entropy - ignored here" |
|
816 |
||
817 |
^ self |
|
818 |
! |
|
819 |
||
2291 | 820 |
seed |
821 |
^ seed |
|
822 |
! |
|
823 |
||
0 | 824 |
setSeed |
3134 | 825 |
"set the initial seed value based on the current time and processId. |
826 |
These numbers implement a maximum period generator which passes |
|
827 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
828 |
the product does not overflow 2 raisedTo:29. |
|
829 |
||
830 |
Use both time and processId for seed, to make different processes |
|
831 |
return different Random numbers" |
|
832 |
||
833 |
|newSeed| |
|
834 |
||
835 |
RandomSalt isNil ifTrue:[ |
|
836 |
RandomSalt := 1. |
|
837 |
] ifFalse:[ |
|
838 |
RandomSalt := RandomSalt + 1. |
|
839 |
]. |
|
840 |
newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId). |
|
841 |
self setSeed:newSeed. |
|
842 |
||
843 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
844 |
! |
|
845 |
||
846 |
setSeed:seedValue |
|
4600 | 847 |
"set the initial seed and intialize the PRNG parameters." |
842 | 848 |
|
4600 | 849 |
self subclassResponsibility. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
850 |
|
1878 | 851 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
852 |
"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
|
853 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
854 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
855 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
856 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
857 |
|
4600 | 858 |
self subclassResponsibility |
0 | 859 |
! ! |
65 | 860 |
|
142 | 861 |
!Random methodsFor:'testing'! |
862 |
||
863 |
atEnd |
|
864 |
"instances of Random can always give more numbers" |
|
865 |
||
866 |
^ false |
|
867 |
! |
|
868 |
||
869 |
isReadable |
|
870 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
871 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
872 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
873 |
isWritable |
3781 | 874 |
"return true, if writing is supported by the receiver. |
875 |
Random Generators never are" |
|
876 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
877 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
878 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
879 |
"Created: 1.4.1997 / 22:38:27 / cg" |
4600 | 880 |
! |
881 |
||
882 |
maxInteger |
|
883 |
"the max value returned by self nextInteger" |
|
884 |
||
885 |
self subclassResponsibility |
|
142 | 886 |
! ! |
887 |
||
888 |
!Random class methodsFor:'documentation'! |
|
889 |
||
890 |
version |
|
3675 | 891 |
^ '$Header$' |
2291 | 892 |
! |
893 |
||
894 |
version_CVS |
|
3675 | 895 |
^ '$Header$' |
142 | 896 |
! ! |
3026 | 897 |