author | Stefan Vogel <sv@exept.de> |
Wed, 08 Apr 2020 19:02:35 +0200 | |
changeset 5473 | de911f462862 |
parent 5448 | a2bd67c9fc67 |
child 5475 | 8e2403d81a18 |
permissions | -rw-r--r-- |
5448 | 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 |
|
5069 | 142 |
next:n |
143 |
"return the next n random numbers 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:n. |
|
150 |
||
151 |
" |
|
152 |
Transcript showCR:(Random next:10). |
|
153 |
" |
|
154 |
! |
|
155 |
||
156 |
next:n between:start and:stop |
|
157 |
"return n random numbers between start and stop. |
|
158 |
This method behaves like the corresponding instance method, |
|
159 |
but allows generation of random numbers without |
|
160 |
a need for an instance of Random to be kept around. |
|
161 |
This uses a common, shared generator." |
|
162 |
||
163 |
^ self sharedGenerator next:n between:start and:stop |
|
164 |
||
165 |
" |
|
166 |
Transcript showCR:(Random next:10 between:1 and:100). |
|
167 |
" |
|
168 |
||
169 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
|
170 |
"Created: 21.8.1997 / 18:09:36 / cg" |
|
171 |
! |
|
172 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
173 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
174 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
175 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
176 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
177 |
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
|
178 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
179 |
|
1128 | 180 |
^ self sharedGenerator nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
181 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
182 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
183 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
184 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
185 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
186 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
187 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
188 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
189 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
190 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
191 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
192 |
|
851 | 193 |
nextBoolean |
194 |
"return a boolean random. |
|
195 |
This method behaves like the corresponding instance method, |
|
196 |
but allows generation of random numbers without |
|
197 |
a need for an instance of Random to be kept around. |
|
198 |
This uses a common, shared generator." |
|
199 |
||
1128 | 200 |
^ self sharedGenerator nextBoolean. |
851 | 201 |
|
202 |
" |
|
203 |
Transcript showCR:(Random nextBoolean). |
|
204 |
Transcript showCR:(Random nextBoolean). |
|
205 |
Transcript showCR:(Random nextBoolean). |
|
206 |
Transcript showCR:(Random nextBoolean). |
|
207 |
" |
|
208 |
||
209 |
"Created: 21.8.1997 / 18:08:23 / cg" |
|
210 |
! |
|
211 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
212 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
213 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
214 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
215 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
216 |
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
|
217 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
218 |
|
1128 | 219 |
^ self sharedGenerator nextInteger. |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
220 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
221 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
222 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
223 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
224 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
225 |
Transcript showCR:(Random nextInteger). |
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 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
228 |
"Created: 21.8.1997 / 18:08:23 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
229 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
230 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
231 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
232 |
"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
|
233 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
234 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
235 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
236 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
237 |
|
1128 | 238 |
^ self sharedGenerator nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
239 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
240 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
241 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
242 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
243 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
244 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
245 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
246 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
247 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
248 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
3750 | 249 |
! |
250 |
||
251 |
nextLettersOrDigits:count |
|
252 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
253 |
||
254 |
^ self sharedGenerator nextLettersOrDigits:count |
|
255 |
||
256 |
" |
|
257 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
258 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
259 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
260 |
Transcript showCR:(Random nextLettersOrDigits:10). |
|
261 |
" |
|
262 |
||
263 |
"Created: 21.8.1997 / 18:07:00 / cg" |
|
264 |
"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
|
265 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
266 |
|
5250 | 267 |
!Random class methodsFor:'reading'! |
268 |
||
269 |
next |
|
270 |
"return the next random number in the range 0..1 |
|
271 |
This method behaves like the corresponding instance method, |
|
272 |
but allows generation of random numbers without |
|
273 |
a need for an instance of Random to be kept around. |
|
274 |
This uses a common, shared generator." |
|
275 |
||
276 |
^ self sharedGenerator next. |
|
277 |
||
278 |
" |
|
279 |
Transcript showCR:(Random next). |
|
280 |
Transcript showCR:(Random next). |
|
281 |
Transcript showCR:(Random next). |
|
282 |
Transcript showCR:(Random next). |
|
283 |
" |
|
284 |
! ! |
|
285 |
||
3383 | 286 |
!Random class methodsFor:'seeding'! |
287 |
||
288 |
randomSeed |
|
289 |
"return a number useful for seeding. |
|
290 |
This takes the current processor's time, plus the processor's process id, |
|
291 |
plus some value depending on the memory allocation state, |
|
3403 | 292 |
plus a random salt, and shuffles those bits around. |
293 |
The entropy returned should be reasonable enough for a good seed of a good rnd |
|
294 |
generator. However, keep in mind, that it only has a limited number of entropy bits |
|
295 |
(in the order of 32). |
|
3414 | 296 |
But it should be much better than what is commonly used in older |
3403 | 297 |
programs (current time) or even a constant." |
3383 | 298 |
|
3414 | 299 |
|hash| |
300 |
||
3383 | 301 |
RandomSalt isNil ifTrue:[ |
302 |
RandomSalt := 1. |
|
303 |
] ifFalse:[ |
|
304 |
RandomSalt := RandomSalt + 1. |
|
305 |
]. |
|
3431 | 306 |
|
307 |
hash := MD5Stream new. |
|
308 |
hash |
|
309 |
nextPut:RandomSalt; |
|
5448 | 310 |
nextPut:OperatingSystem getMicrosecondTime; |
3431 | 311 |
nextPut:OperatingSystem getProcessId; |
312 |
nextPut:(ObjectMemory addressOf:Object new); |
|
313 |
nextPut:ObjectMemory oldSpaceUsed; |
|
4198 | 314 |
nextPut:ObjectMemory newSpaceUsed; |
315 |
nextPut:OperatingSystem getCPUCycleCount. |
|
3403 | 316 |
|
3414 | 317 |
"/ any other cheap sources of entropy? |
3403 | 318 |
|
4198 | 319 |
"/ I think there is no problem in that MD5 is not a secure hash algo here |
320 |
"/ - the idea is to shuffle the bits around a bit |
|
3403 | 321 |
"/ (because the numbers above usually have many high bits in common) |
322 |
"/ and then condense the bits into a smaller number. |
|
323 |
"/ Any comment from a crypto guy here - I am willing to change this to some other hash, if that makes a problem |
|
324 |
||
3414 | 325 |
"/ Seeding rnd generators should take some bits from the returned number (i.e. their max. seed size) |
326 |
^ LargeInteger digitBytes:hash hashValue. |
|
3383 | 327 |
|
328 |
" |
|
3403 | 329 |
10 timesRepeat:[Transcript showCR:self randomSeed]. |
3414 | 330 |
10 timesRepeat:[Transcript showCR:(self randomSeed bitAnd:16rFFFF)]. |
3403 | 331 |
self randomSeed bitAnd:16rFFFFFFFF |
3383 | 332 |
" |
333 |
! ! |
|
334 |
||
518 | 335 |
!Random class methodsFor:'testing'! |
336 |
||
793 | 337 |
bucketTest: randy |
338 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
339 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
340 |
indicates something is very wrong. Each run generates different values. |
|
341 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
342 |
go get coffee. |
|
343 |
This is a poor test; see Knuth. |
|
344 |
Some 'OK' runs: |
|
345 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
346 |
1011 987 982 980 982 974 968 1044 976 |
|
347 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
348 |
978 1035 995 988 1038 1009 988 993 976 |
|
349 |
" |
|
350 |
||
351 |
| nbuckets buckets ntrys slot | |
|
352 |
||
353 |
nbuckets := 20. |
|
3192 | 354 |
buckets := Array new: nbuckets withAll:0. |
793 | 355 |
ntrys := 1000. |
356 |
ntrys*nbuckets timesRepeat: [ |
|
357 |
slot := (randy next * nbuckets) floor + 1. |
|
358 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
359 |
Transcript cr. |
|
360 |
1 to: nbuckets do: [ :nb | |
|
361 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
362 |
||
363 |
||
364 |
"Execute this: |
|
3192 | 365 |
self bucketTest: self new |
366 |
self bucketTest: RandomGenerator new |
|
793 | 367 |
" |
368 |
! |
|
369 |
||
518 | 370 |
chiSquareTest |
371 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
372 |
o N = number of samples |
|
373 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
374 |
o Random number generator 'passes' if chisquare value is very close to r |
|
375 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
376 |
||
3192 | 377 |
| aGenerator frequencies n range t | |
378 |
||
379 |
aGenerator := self new. "Seeded differently each time (if seeded at all)" |
|
380 |
range := 100. |
|
381 |
n := 10000. |
|
382 |
frequencies := Array new:range withAll:0. |
|
518 | 383 |
|
3192 | 384 |
1 to: n do: [:i | |
385 |
t := ((aGenerator next) * range) truncated + 1. |
|
386 |
frequencies at:t put: ((frequencies at:t) + 1). |
|
518 | 387 |
]. |
3192 | 388 |
t := frequencies inject:0 into: [:nextValue :eachFreq | |
389 |
nextValue + eachFreq squared |
|
390 |
]. |
|
391 |
^ ((range * t / n) - n) asFloat. |
|
518 | 392 |
|
393 |
" |
|
3192 | 394 |
self chiSquareTest |
395 |
RandomGenerator chiSquareTest |
|
396 |
" |
|
397 |
||
398 |
" |
|
399 |
|fail| |
|
400 |
fail := 0. |
|
401 |
10 timesRepeat:[ |
|
402 |
|testResult| |
|
403 |
testResult := RandomGenerator chiSquareTest. |
|
404 |
(100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1]. |
|
405 |
]. |
|
406 |
fail > 1 ifTrue:[self error:'test failed']. |
|
518 | 407 |
" |
408 |
||
409 |
" |
|
410 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
411 |
The closer to 100, the better. |
|
412 |
" |
|
413 |
! ! |
|
414 |
||
1201 | 415 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 416 |
|
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
417 |
nextInt:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
418 |
"Answer a random integer in the interval [1, anInteger]." |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
419 |
|
3188 | 420 |
(upperBound < 1) ifTrue:[self error:'invalid upper bound']. |
421 |
||
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
422 |
^ self nextIntegerBetween:1 and:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
423 |
|
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
424 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
425 |
Random new nextInt:10 |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
426 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
427 |
! |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
428 |
|
928 | 429 |
nextIntFrom:lowerBound to:upperBound |
430 |
"return a random integer in the given range" |
|
431 |
||
432 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
433 |
||
434 |
" |
|
435 |
Random new nextIntFrom:5 to:10 |
|
436 |
" |
|
3060
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
437 |
! |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
438 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
439 |
seed: anInteger |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
440 |
self setSeed: anInteger. |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
441 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
442 |
"Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
928 | 443 |
! ! |
444 |
||
65 | 445 |
!Random methodsFor:'accessing-reading'! |
0 | 446 |
|
4640 | 447 |
next:count |
448 |
"return the next count random numbers in the range ]0..1[" |
|
449 |
||
450 |
|answerStream |
|
451 |
cnt "{ Class: SmallInteger }" | |
|
452 |
||
453 |
cnt := count. |
|
454 |
answerStream := self contentsSpecies writeStream:cnt. |
|
455 |
1 to:cnt do:[:index | |next| |
|
456 |
next := self next. |
|
457 |
answerStream nextPut:next. |
|
458 |
]. |
|
459 |
^ answerStream contents |
|
460 |
||
461 |
" |
|
462 |
(RandomGenerator new) next:10 |
|
463 |
" |
|
464 |
! |
|
465 |
||
5069 | 466 |
next:count between:min and:max |
467 |
"return the next count random numbers in min..max" |
|
468 |
||
469 |
|answerStream |
|
470 |
cnt "{ Class: SmallInteger }" | |
|
471 |
||
472 |
cnt := count. |
|
473 |
answerStream := self contentsSpecies writeStream:cnt. |
|
474 |
1 to:cnt do:[:index | |next| |
|
475 |
next := self nextBetween:min and:max. |
|
476 |
answerStream nextPut:next. |
|
477 |
]. |
|
478 |
^ answerStream contents |
|
479 |
||
480 |
" |
|
481 |
(RandomGenerator new) next:10 between:0 and:100 |
|
482 |
" |
|
483 |
! |
|
484 |
||
4640 | 485 |
next:count integerBetween:min and:max |
486 |
"return the next count random integers in min..max" |
|
487 |
||
488 |
|answerStream |
|
489 |
cnt "{ Class: SmallInteger }" | |
|
490 |
||
491 |
cnt := count. |
|
492 |
answerStream := self contentsSpecies writeStream:cnt. |
|
493 |
1 to:cnt do:[:index | |next| |
|
494 |
next := self nextIntegerBetween:min and:max. |
|
495 |
answerStream nextPut:next. |
|
496 |
]. |
|
497 |
^ answerStream contents |
|
498 |
||
499 |
" |
|
500 |
(RandomGenerator new) next:100 integerBetween:0 and:100 |
|
501 |
" |
|
502 |
! |
|
503 |
||
65 | 504 |
nextBetween:start and:stop |
3675 | 505 |
"return a random number in the range ]start..stop[. |
65 | 506 |
claus: the original GNU version has a bug in returning values |
507 |
from the interval [start .. stop+1]" |
|
508 |
||
4627 | 509 |
|rndFloat rnd| |
65 | 510 |
|
4627 | 511 |
rndFloat := self next. |
512 |
rnd := (rndFloat * (stop - start) asFloat) + start asFloat. |
|
65 | 513 |
^ rnd |
514 |
||
4614 | 515 |
" |
516 |
|r| |
|
65 | 517 |
r := Random new. |
350 | 518 |
Transcript showCR:(r nextBetween:1 and:10). |
519 |
Transcript showCR:(r nextBetween:1 and:10). |
|
520 |
Transcript showCR:(r nextBetween:1 and:10). |
|
521 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 522 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
523 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
524 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 525 |
! |
526 |
||
0 | 527 |
nextBoolean |
17 | 528 |
"return true or false by random" |
529 |
||
4614 | 530 |
^ self nextInteger bitAnd:16r10. |
17 | 531 |
|
3134 | 532 |
" |
533 |
|r| |
|
17 | 534 |
r := Random new. |
350 | 535 |
Transcript showCR:r nextBoolean. |
536 |
Transcript showCR:r nextBoolean. |
|
537 |
Transcript showCR:r nextBoolean. |
|
538 |
Transcript showCR:r nextBoolean. |
|
17 | 539 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
540 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
541 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
542 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
543 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
544 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
545 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
546 |
bag add:(r nextBoolean). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
547 |
]. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
548 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
549 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
550 |
|
2045 | 551 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 552 |
! |
553 |
||
840 | 554 |
nextByte |
555 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
556 |
||
4614 | 557 |
^ self nextInteger bitAnd:16rFF |
840 | 558 |
|
559 |
" |
|
560 |
|r| |
|
3134 | 561 |
r := self new. |
840 | 562 |
Transcript showCR:r nextByte. |
563 |
Transcript showCR:r nextByte. |
|
564 |
Transcript showCR:r nextByte. |
|
565 |
Transcript showCR:r nextByte. |
|
566 |
" |
|
567 |
||
568 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
569 |
! |
|
570 |
||
571 |
nextBytes:count |
|
978 | 572 |
"return count random bytes (0..16rFF each)" |
840 | 573 |
|
4600 | 574 |
^ (1 to:count) collect:[:i | self nextByte] as:ByteArray |
840 | 575 |
|
576 |
" |
|
4600 | 577 |
Transcript showCR:(RandomGenerator new nextBytes:20). |
840 | 578 |
" |
579 |
||
580 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
581 |
! |
|
582 |
||
4599 | 583 |
nextBytesNonZero:count |
584 |
"return count random bytes, none of which is zero (i.e. 1..16rFF each)" |
|
585 |
||
586 |
|res cnt "{Class: SmallInteger}"| |
|
587 |
||
588 |
cnt := count. |
|
589 |
res := ByteArray uninitializedNew:cnt. |
|
590 |
||
591 |
1 to:cnt do:[:i| |
|
592 |
|nextNonZero| |
|
593 |
||
594 |
[ |
|
4611 | 595 |
(nextNonZero := self nextByte) == 0 |
4599 | 596 |
] whileTrue. |
597 |
res at:i put:nextNonZero. |
|
598 |
]. |
|
599 |
||
600 |
^ res |
|
601 |
||
602 |
" |
|
603 |
Transcript showCR:(Random new nextBytesNonZero:20). |
|
604 |
" |
|
605 |
||
606 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
607 |
! |
|
608 |
||
1642 | 609 |
nextCharacters:count |
610 |
"get the next cnt printable characters. |
|
3750 | 611 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
1642 | 612 |
|
4614 | 613 |
^ (1 to:count) collect:[:i | Character value:(self nextInteger \\ 94 + 32) ] as:String |
1642 | 614 |
|
615 |
" |
|
4600 | 616 |
RandomGenerator new nextCharacters:8 |
1642 | 617 |
" |
618 |
! |
|
619 |
||
5214 | 620 |
nextHumanReadableStringOfSize:length |
621 |
|s canVowel canConsonant followers| |
|
622 |
||
623 |
followers := |
|
624 |
#( |
|
625 |
'a' "->" 'uibcdfgklmnpqrstvwxz' |
|
626 |
'b' "->" 'aeiou' |
|
627 |
'c' "->" 'aeioukq' |
|
628 |
'd' "->" 'aeiou' |
|
629 |
'e' "->" 'uibcdfgklmnpqrstvwxz' |
|
630 |
'f' "->" 'aeiou' |
|
631 |
'g' "->" 'aeiou' |
|
632 |
'h' "->" 'aeiou' |
|
633 |
'i' "->" 'bcdfgklmnpqrstvwxz' |
|
634 |
'j' "->" 'aeiou' |
|
635 |
'k' "->" 'aeiou' |
|
636 |
'l' "->" 'aeiou' |
|
637 |
'm' "->" 'aeiou' |
|
638 |
'n' "->" 'aeiou' |
|
639 |
'o' "->" 'uabcdfgklmnpqrstvwxz' |
|
640 |
'p' "->" 'aeiou' |
|
641 |
'q' "->" 'u' |
|
642 |
'r' "->" 'aeiou' |
|
643 |
's' "->" 'aeiou' |
|
644 |
't' "->" 'aeiouh' |
|
645 |
'u' "->" 'ibcdfgklmnpqrstvwxz' |
|
646 |
'v' "->" 'aeiou' |
|
647 |
'w' "->" 'aeiou' |
|
648 |
'x' "->" 'aeiou' |
|
649 |
'y' "->" 'aeiou' |
|
650 |
'z' "->" 'aeiou' |
|
651 |
). |
|
652 |
||
653 |
s := ''. |
|
654 |
canVowel := canConsonant := true. |
|
655 |
length timesRepeat:[ |
|
656 |
|chars ch| |
|
657 |
||
658 |
s isEmpty ifTrue:[ |
|
659 |
chars := $a to: $z. |
|
660 |
] ifFalse:[ |
|
661 |
chars := followers |
|
662 |
inGroupsOf:2 |
|
663 |
detect:[:prefix :charSet | s endsWith:prefix ] |
|
664 |
thenDo:[:prefix :charSet | charSet ] |
|
665 |
ifNone:[self error]. |
|
666 |
]. |
|
667 |
ch := chars atRandom. |
|
668 |
s := s , ch. |
|
669 |
]. |
|
670 |
^ s contents. |
|
671 |
||
672 |
" |
|
673 |
(1 to:1000) collect:[:n | Random new nextHumanReadableStringOfSize:6 ] as:Bag |
|
674 |
" |
|
675 |
! |
|
676 |
||
142 | 677 |
nextInteger |
678 |
"return the next integral random number, |
|
4600 | 679 |
in the range 0 .. self maxInteger" |
3134 | 680 |
|
4600 | 681 |
self subclassResponsibility |
142 | 682 |
|
840 | 683 |
" |
684 |
|r| |
|
142 | 685 |
r := Random new. |
350 | 686 |
Transcript showCR:r nextInteger. |
687 |
Transcript showCR:r nextInteger. |
|
688 |
Transcript showCR:r nextInteger. |
|
689 |
Transcript showCR:r nextInteger. |
|
142 | 690 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
691 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
692 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 693 |
! |
694 |
||
17 | 695 |
nextIntegerBetween:start and:stop |
4600 | 696 |
"return an integral random number in [start..stop] (inclusive)" |
17 | 697 |
|
3026 | 698 |
|rnd range bytesNeeded| |
17 | 699 |
|
3026 | 700 |
range := stop - start + 1. |
4600 | 701 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
702 |
bytesNeeded := (range highBit + 15) // 8. |
|
703 |
bytesNeeded < 4 ifTrue:[ |
|
704 |
rnd := (((self nextByte bitShift:8) + self nextByte) bitShift:8) + self nextByte. |
|
3134 | 705 |
] ifFalse:[ |
4600 | 706 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
3134 | 707 |
]. |
5201 | 708 |
rnd := rnd \\ range. |
4600 | 709 |
^ rnd + start |
710 |
||
711 |
"shows pair-combination counts |
|
712 |
||
713 |
|r v prev this counts| |
|
714 |
||
715 |
v := TextView new . |
|
716 |
v extent:500@250. |
|
717 |
v list:#('xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
718 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
719 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
720 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
721 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
722 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
723 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
724 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
725 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
726 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'). |
|
727 |
v openAndWait. |
|
728 |
counts := (1 to:10) collect:[:row | Array new:10 withAll:0]. |
|
729 |
||
730 |
r := self new. |
|
731 |
prev := r nextIntegerBetween:1 and:10. |
|
732 |
10000 timesRepeat:[ |
|
733 |
100 timesRepeat:[ |
|
734 |
this := r nextIntegerBetween:1 and:10. |
|
735 |
(counts at:this) at:prev put:((counts at:this) at:prev)+1. |
|
736 |
]. |
|
737 |
v list:(counts collect:[:row | |
|
738 |
'%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d'printf:row ]). |
|
739 |
prev := this. |
|
740 |
]. |
|
741 |
" |
|
17 | 742 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
743 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
744 |
|r| |
3026 | 745 |
r := self new. |
350 | 746 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
747 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
748 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
749 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 750 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
751 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
752 |
" |
3134 | 753 |
|r| |
754 |
r := self new. |
|
755 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
756 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
757 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
758 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
759 |
" |
|
760 |
||
761 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
762 |
|r bag| |
3026 | 763 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
764 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
765 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
766 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
767 |
]. |
3134 | 768 |
Transcript showCR:bag sortedCounts. |
3026 | 769 |
" |
770 |
||
771 |
" |
|
772 |
|r bag| |
|
773 |
r := self new. |
|
774 |
bag := Bag new. |
|
775 |
1000000 timesRepeat:[ |
|
776 |
bag add:(r nextIntegerBetween:1 and:3). |
|
777 |
]. |
|
3134 | 778 |
Transcript showCR:bag sortedCounts. |
3026 | 779 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
780 |
" |
|
781 |
||
782 |
" |
|
783 |
|r bag| |
|
784 |
r := self new. |
|
785 |
bag := Bag new. |
|
786 |
1000000 timesRepeat:[ |
|
3134 | 787 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 788 |
]. |
3134 | 789 |
Transcript showCR:bag sortedCounts. |
790 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
791 |
" |
|
792 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
793 |
" |
3134 | 794 |
|r bag| |
795 |
r := self new. |
|
796 |
bag := Bag new. |
|
797 |
100000000 timesRepeat:[ |
|
798 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
799 |
]. |
|
800 |
Transcript showCR:bag sortedCounts. |
|
801 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
802 |
" |
|
803 |
||
3675 | 804 |
" |
805 |
|r| |
|
806 |
||
807 |
r := self new. |
|
808 |
100000000 timesRepeat:[ |
|
809 |
self assert:((r nextIntegerBetween:1 and:3) between:1 and:3). |
|
810 |
]. |
|
811 |
" |
|
3750 | 812 |
! |
813 |
||
814 |
nextLettersOrDigits:count |
|
4600 | 815 |
"get a random string consisting of count printable letters or digits [0-9A-Za-z]." |
3750 | 816 |
|
817 |
|res cnt "{ Class:SmallInteger }"| |
|
818 |
||
819 |
cnt := count. |
|
820 |
res := String uninitializedNew:cnt. |
|
821 |
||
822 |
1 to:cnt do:[:i| |
|
823 |
self step. |
|
824 |
res |
|
825 |
at:i |
|
826 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)). |
|
827 |
]. |
|
828 |
||
829 |
^ res |
|
830 |
||
831 |
" |
|
4600 | 832 |
Random new nextLettersOrDigits:40 |
3750 | 833 |
" |
0 | 834 |
! ! |
835 |
||
65 | 836 |
!Random methodsFor:'blocked methods'! |
0 | 837 |
|
65 | 838 |
contents |
839 |
"blocked from use - contents makes no sense for random generators" |
|
840 |
||
841 |
self shouldNotImplement |
|
842 |
! |
|
843 |
||
844 |
nextPut: value |
|
845 |
"blocked from use - it makes no sense for randoms" |
|
846 |
||
847 |
self shouldNotImplement |
|
848 |
! ! |
|
849 |
||
2459 | 850 |
!Random methodsFor:'initialization'! |
851 |
||
852 |
initialize |
|
853 |
self setSeed |
|
854 |
! ! |
|
855 |
||
65 | 856 |
!Random methodsFor:'private'! |
0 | 857 |
|
2430 | 858 |
addEntropy:entropyBytes |
859 |
"add some entropy - ignored here" |
|
860 |
||
861 |
^ self |
|
862 |
! |
|
863 |
||
2291 | 864 |
seed |
865 |
^ seed |
|
866 |
! |
|
867 |
||
0 | 868 |
setSeed |
3134 | 869 |
"set the initial seed value based on the current time and processId. |
870 |
These numbers implement a maximum period generator which passes |
|
871 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
872 |
the product does not overflow 2 raisedTo:29. |
|
873 |
||
874 |
Use both time and processId for seed, to make different processes |
|
875 |
return different Random numbers" |
|
876 |
||
877 |
|newSeed| |
|
878 |
||
879 |
RandomSalt isNil ifTrue:[ |
|
880 |
RandomSalt := 1. |
|
881 |
] ifFalse:[ |
|
882 |
RandomSalt := RandomSalt + 1. |
|
883 |
]. |
|
884 |
newSeed := RandomSalt + (Time millisecondClockValue bitXor:OperatingSystem getProcessId). |
|
885 |
self setSeed:newSeed. |
|
886 |
||
887 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
888 |
! |
|
889 |
||
890 |
setSeed:seedValue |
|
4600 | 891 |
"set the initial seed and intialize the PRNG parameters." |
842 | 892 |
|
4600 | 893 |
self subclassResponsibility. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
894 |
|
1878 | 895 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
896 |
"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
|
897 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
898 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
899 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
900 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
901 |
|
4600 | 902 |
self subclassResponsibility |
0 | 903 |
! ! |
65 | 904 |
|
5250 | 905 |
!Random methodsFor:'reading'! |
906 |
||
907 |
next |
|
908 |
"return the next random number in the range ]0..1[" |
|
909 |
||
910 |
self subclassResponsibility. |
|
911 |
||
912 |
" |
|
913 |
|r| |
|
914 |
r := Random new. |
|
915 |
Transcript showCR:r next. |
|
916 |
Transcript showCR:r next. |
|
917 |
Transcript showCR:r next. |
|
918 |
Transcript showCR:r next. |
|
919 |
" |
|
920 |
||
921 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
|
922 |
! ! |
|
923 |
||
142 | 924 |
!Random methodsFor:'testing'! |
925 |
||
926 |
atEnd |
|
927 |
"instances of Random can always give more numbers" |
|
928 |
||
929 |
^ false |
|
930 |
! |
|
931 |
||
932 |
isReadable |
|
933 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
934 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
935 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
936 |
isWritable |
3781 | 937 |
"return true, if writing is supported by the receiver. |
938 |
Random Generators never are" |
|
939 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
940 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
941 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
942 |
"Created: 1.4.1997 / 22:38:27 / cg" |
4600 | 943 |
! |
944 |
||
945 |
maxInteger |
|
946 |
"the max value returned by self nextInteger" |
|
947 |
||
948 |
self subclassResponsibility |
|
142 | 949 |
! ! |
950 |
||
951 |
!Random class methodsFor:'documentation'! |
|
952 |
||
953 |
version |
|
3675 | 954 |
^ '$Header$' |
2291 | 955 |
! |
956 |
||
957 |
version_CVS |
|
3675 | 958 |
^ '$Header$' |
142 | 959 |
! ! |
3026 | 960 |