author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 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 |
|
5475 | 307 |
hash := SHA1Stream new. |
3431 | 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 |
|
5475 | 319 |
"/ I think there is no problem in that SHA1 is not a secure hash algo here |
4198 | 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 |
" |
5475 | 333 |
|
334 |
"Modified: / 16-04-2020 / 17:06:52 / Stefan Vogel" |
|
3383 | 335 |
! ! |
336 |
||
518 | 337 |
!Random class methodsFor:'testing'! |
338 |
||
793 | 339 |
bucketTest: randy |
340 |
"A quick-and-dirty bucket test. Prints nbuckets values on the Transcript. |
|
341 |
Each should be 'near' the value of ntries. Any run with any value 'far' from ntries |
|
342 |
indicates something is very wrong. Each run generates different values. |
|
343 |
For a slightly better test, try values of nbuckets of 200-1000 or more; |
|
344 |
go get coffee. |
|
345 |
This is a poor test; see Knuth. |
|
346 |
Some 'OK' runs: |
|
347 |
1000 1023 998 969 997 1018 1030 1019 1054 985 1003 |
|
348 |
1011 987 982 980 982 974 968 1044 976 |
|
349 |
1029 1011 1025 1016 997 1019 991 954 968 999 991 |
|
350 |
978 1035 995 988 1038 1009 988 993 976 |
|
351 |
" |
|
352 |
||
353 |
| nbuckets buckets ntrys slot | |
|
354 |
||
355 |
nbuckets := 20. |
|
3192 | 356 |
buckets := Array new: nbuckets withAll:0. |
793 | 357 |
ntrys := 1000. |
358 |
ntrys*nbuckets timesRepeat: [ |
|
359 |
slot := (randy next * nbuckets) floor + 1. |
|
360 |
buckets at: slot put: (buckets at: slot) + 1 ]. |
|
361 |
Transcript cr. |
|
362 |
1 to: nbuckets do: [ :nb | |
|
363 |
Transcript show: (buckets at: nb) printString, ' ' ] |
|
364 |
||
365 |
||
366 |
"Execute this: |
|
3192 | 367 |
self bucketTest: self new |
368 |
self bucketTest: RandomGenerator new |
|
793 | 369 |
" |
370 |
! |
|
371 |
||
518 | 372 |
chiSquareTest |
373 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
374 |
o N = number of samples |
|
375 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
376 |
o Random number generator 'passes' if chisquare value is very close to r |
|
377 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
378 |
||
3192 | 379 |
| aGenerator frequencies n range t | |
380 |
||
381 |
aGenerator := self new. "Seeded differently each time (if seeded at all)" |
|
382 |
range := 100. |
|
383 |
n := 10000. |
|
384 |
frequencies := Array new:range withAll:0. |
|
518 | 385 |
|
3192 | 386 |
1 to: n do: [:i | |
387 |
t := ((aGenerator next) * range) truncated + 1. |
|
388 |
frequencies at:t put: ((frequencies at:t) + 1). |
|
518 | 389 |
]. |
3192 | 390 |
t := frequencies inject:0 into: [:nextValue :eachFreq | |
391 |
nextValue + eachFreq squared |
|
392 |
]. |
|
393 |
^ ((range * t / n) - n) asFloat. |
|
518 | 394 |
|
395 |
" |
|
3192 | 396 |
self chiSquareTest |
397 |
RandomGenerator chiSquareTest |
|
398 |
" |
|
399 |
||
400 |
" |
|
401 |
|fail| |
|
402 |
fail := 0. |
|
403 |
10 timesRepeat:[ |
|
404 |
|testResult| |
|
405 |
testResult := RandomGenerator chiSquareTest. |
|
406 |
(100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1]. |
|
407 |
]. |
|
408 |
fail > 1 ifTrue:[self error:'test failed']. |
|
518 | 409 |
" |
410 |
||
411 |
" |
|
412 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
413 |
The closer to 100, the better. |
|
414 |
" |
|
415 |
! ! |
|
416 |
||
1201 | 417 |
!Random methodsFor:'Compatibility-Squeak'! |
928 | 418 |
|
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
419 |
nextInt:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
420 |
"Answer a random integer in the interval [1, anInteger]." |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
421 |
|
3188 | 422 |
(upperBound < 1) ifTrue:[self error:'invalid upper bound']. |
423 |
||
2309
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
424 |
^ self nextIntegerBetween:1 and:upperBound |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
425 |
|
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 |
Random new nextInt:10 |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
428 |
" |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
429 |
! |
9f2c39c721db
added: #nextInt: - squeak compatibility
Claus Gittinger <cg@exept.de>
parents:
2291
diff
changeset
|
430 |
|
928 | 431 |
nextIntFrom:lowerBound to:upperBound |
432 |
"return a random integer in the given range" |
|
433 |
||
434 |
^ self nextIntegerBetween:lowerBound and:upperBound |
|
435 |
||
436 |
" |
|
437 |
Random new nextIntFrom:5 to:10 |
|
438 |
" |
|
3060
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
439 |
! |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
440 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
441 |
seed: anInteger |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
442 |
self setSeed: anInteger. |
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
443 |
|
5267b3c2754c
added: #seed: for compatibility with Squeak
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3026
diff
changeset
|
444 |
"Created: / 20-07-2013 / 01:52:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
928 | 445 |
! ! |
446 |
||
65 | 447 |
!Random methodsFor:'accessing-reading'! |
0 | 448 |
|
4640 | 449 |
next:count |
450 |
"return the next count random numbers in the range ]0..1[" |
|
451 |
||
452 |
|answerStream |
|
453 |
cnt "{ Class: SmallInteger }" | |
|
454 |
||
455 |
cnt := count. |
|
456 |
answerStream := self contentsSpecies writeStream:cnt. |
|
457 |
1 to:cnt do:[:index | |next| |
|
458 |
next := self next. |
|
459 |
answerStream nextPut:next. |
|
460 |
]. |
|
461 |
^ answerStream contents |
|
462 |
||
463 |
" |
|
464 |
(RandomGenerator new) next:10 |
|
465 |
" |
|
466 |
! |
|
467 |
||
5069 | 468 |
next:count between:min and:max |
469 |
"return the next count random numbers in min..max" |
|
470 |
||
471 |
|answerStream |
|
472 |
cnt "{ Class: SmallInteger }" | |
|
473 |
||
474 |
cnt := count. |
|
475 |
answerStream := self contentsSpecies writeStream:cnt. |
|
476 |
1 to:cnt do:[:index | |next| |
|
477 |
next := self nextBetween:min and:max. |
|
478 |
answerStream nextPut:next. |
|
479 |
]. |
|
480 |
^ answerStream contents |
|
481 |
||
482 |
" |
|
483 |
(RandomGenerator new) next:10 between:0 and:100 |
|
484 |
" |
|
485 |
! |
|
486 |
||
4640 | 487 |
next:count integerBetween:min and:max |
488 |
"return the next count random integers in min..max" |
|
489 |
||
490 |
|answerStream |
|
491 |
cnt "{ Class: SmallInteger }" | |
|
492 |
||
493 |
cnt := count. |
|
494 |
answerStream := self contentsSpecies writeStream:cnt. |
|
495 |
1 to:cnt do:[:index | |next| |
|
496 |
next := self nextIntegerBetween:min and:max. |
|
497 |
answerStream nextPut:next. |
|
498 |
]. |
|
499 |
^ answerStream contents |
|
500 |
||
501 |
" |
|
502 |
(RandomGenerator new) next:100 integerBetween:0 and:100 |
|
503 |
" |
|
504 |
! |
|
505 |
||
65 | 506 |
nextBetween:start and:stop |
3675 | 507 |
"return a random number in the range ]start..stop[. |
65 | 508 |
claus: the original GNU version has a bug in returning values |
509 |
from the interval [start .. stop+1]" |
|
510 |
||
4627 | 511 |
|rndFloat rnd| |
65 | 512 |
|
4627 | 513 |
rndFloat := self next. |
514 |
rnd := (rndFloat * (stop - start) asFloat) + start asFloat. |
|
65 | 515 |
^ rnd |
516 |
||
4614 | 517 |
" |
518 |
|r| |
|
65 | 519 |
r := Random new. |
350 | 520 |
Transcript showCR:(r nextBetween:1 and:10). |
521 |
Transcript showCR:(r nextBetween:1 and:10). |
|
522 |
Transcript showCR:(r nextBetween:1 and:10). |
|
523 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 524 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
525 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
526 |
"Modified: / 21.8.1998 / 14:45:27 / cg" |
65 | 527 |
! |
528 |
||
0 | 529 |
nextBoolean |
17 | 530 |
"return true or false by random" |
531 |
||
4614 | 532 |
^ self nextInteger bitAnd:16r10. |
17 | 533 |
|
3134 | 534 |
" |
535 |
|r| |
|
17 | 536 |
r := Random new. |
350 | 537 |
Transcript showCR:r nextBoolean. |
538 |
Transcript showCR:r nextBoolean. |
|
539 |
Transcript showCR:r nextBoolean. |
|
540 |
Transcript showCR:r nextBoolean. |
|
17 | 541 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
542 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
543 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
544 |
|r bag| |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
545 |
r := Random new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
546 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
547 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
548 |
bag add:(r nextBoolean). |
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 |
Transcript showCR:bag contents |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
551 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
552 |
|
2045 | 553 |
"Modified: / 22-10-2008 / 15:17:57 / cg" |
0 | 554 |
! |
555 |
||
840 | 556 |
nextByte |
557 |
"return the next integral random number byte in the range 0 .. 16rFF" |
|
558 |
||
4614 | 559 |
^ self nextInteger bitAnd:16rFF |
840 | 560 |
|
561 |
" |
|
562 |
|r| |
|
3134 | 563 |
r := self new. |
840 | 564 |
Transcript showCR:r nextByte. |
565 |
Transcript showCR:r nextByte. |
|
566 |
Transcript showCR:r nextByte. |
|
567 |
Transcript showCR:r nextByte. |
|
568 |
" |
|
569 |
||
570 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
571 |
! |
|
572 |
||
573 |
nextBytes:count |
|
978 | 574 |
"return count random bytes (0..16rFF each)" |
840 | 575 |
|
4600 | 576 |
^ (1 to:count) collect:[:i | self nextByte] as:ByteArray |
840 | 577 |
|
578 |
" |
|
4600 | 579 |
Transcript showCR:(RandomGenerator new nextBytes:20). |
840 | 580 |
" |
581 |
||
582 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
583 |
! |
|
584 |
||
4599 | 585 |
nextBytesNonZero:count |
586 |
"return count random bytes, none of which is zero (i.e. 1..16rFF each)" |
|
587 |
||
588 |
|res cnt "{Class: SmallInteger}"| |
|
589 |
||
590 |
cnt := count. |
|
591 |
res := ByteArray uninitializedNew:cnt. |
|
592 |
||
593 |
1 to:cnt do:[:i| |
|
594 |
|nextNonZero| |
|
595 |
||
596 |
[ |
|
4611 | 597 |
(nextNonZero := self nextByte) == 0 |
4599 | 598 |
] whileTrue. |
599 |
res at:i put:nextNonZero. |
|
600 |
]. |
|
601 |
||
602 |
^ res |
|
603 |
||
604 |
" |
|
605 |
Transcript showCR:(Random new nextBytesNonZero:20). |
|
606 |
" |
|
607 |
||
608 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
|
609 |
! |
|
610 |
||
1642 | 611 |
nextCharacters:count |
612 |
"get the next cnt printable characters. |
|
3750 | 613 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
1642 | 614 |
|
4614 | 615 |
^ (1 to:count) collect:[:i | Character value:(self nextInteger \\ 94 + 32) ] as:String |
1642 | 616 |
|
617 |
" |
|
4600 | 618 |
RandomGenerator new nextCharacters:8 |
1642 | 619 |
" |
620 |
! |
|
621 |
||
5214 | 622 |
nextHumanReadableStringOfSize:length |
623 |
|s canVowel canConsonant followers| |
|
624 |
||
625 |
followers := |
|
626 |
#( |
|
627 |
'a' "->" 'uibcdfgklmnpqrstvwxz' |
|
628 |
'b' "->" 'aeiou' |
|
629 |
'c' "->" 'aeioukq' |
|
630 |
'd' "->" 'aeiou' |
|
631 |
'e' "->" 'uibcdfgklmnpqrstvwxz' |
|
632 |
'f' "->" 'aeiou' |
|
633 |
'g' "->" 'aeiou' |
|
634 |
'h' "->" 'aeiou' |
|
635 |
'i' "->" 'bcdfgklmnpqrstvwxz' |
|
636 |
'j' "->" 'aeiou' |
|
637 |
'k' "->" 'aeiou' |
|
638 |
'l' "->" 'aeiou' |
|
639 |
'm' "->" 'aeiou' |
|
640 |
'n' "->" 'aeiou' |
|
641 |
'o' "->" 'uabcdfgklmnpqrstvwxz' |
|
642 |
'p' "->" 'aeiou' |
|
643 |
'q' "->" 'u' |
|
644 |
'r' "->" 'aeiou' |
|
645 |
's' "->" 'aeiou' |
|
646 |
't' "->" 'aeiouh' |
|
647 |
'u' "->" 'ibcdfgklmnpqrstvwxz' |
|
648 |
'v' "->" 'aeiou' |
|
649 |
'w' "->" 'aeiou' |
|
650 |
'x' "->" 'aeiou' |
|
651 |
'y' "->" 'aeiou' |
|
652 |
'z' "->" 'aeiou' |
|
653 |
). |
|
654 |
||
655 |
s := ''. |
|
656 |
canVowel := canConsonant := true. |
|
657 |
length timesRepeat:[ |
|
658 |
|chars ch| |
|
659 |
||
660 |
s isEmpty ifTrue:[ |
|
661 |
chars := $a to: $z. |
|
662 |
] ifFalse:[ |
|
663 |
chars := followers |
|
664 |
inGroupsOf:2 |
|
665 |
detect:[:prefix :charSet | s endsWith:prefix ] |
|
666 |
thenDo:[:prefix :charSet | charSet ] |
|
667 |
ifNone:[self error]. |
|
668 |
]. |
|
669 |
ch := chars atRandom. |
|
670 |
s := s , ch. |
|
671 |
]. |
|
672 |
^ s contents. |
|
673 |
||
674 |
" |
|
675 |
(1 to:1000) collect:[:n | Random new nextHumanReadableStringOfSize:6 ] as:Bag |
|
676 |
" |
|
677 |
! |
|
678 |
||
142 | 679 |
nextInteger |
680 |
"return the next integral random number, |
|
4600 | 681 |
in the range 0 .. self maxInteger" |
3134 | 682 |
|
4600 | 683 |
self subclassResponsibility |
142 | 684 |
|
840 | 685 |
" |
686 |
|r| |
|
142 | 687 |
r := Random new. |
350 | 688 |
Transcript showCR:r nextInteger. |
689 |
Transcript showCR:r nextInteger. |
|
690 |
Transcript showCR:r nextInteger. |
|
691 |
Transcript showCR:r nextInteger. |
|
142 | 692 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
693 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
694 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 695 |
! |
696 |
||
17 | 697 |
nextIntegerBetween:start and:stop |
4600 | 698 |
"return an integral random number in [start..stop] (inclusive)" |
17 | 699 |
|
3026 | 700 |
|rnd range bytesNeeded| |
17 | 701 |
|
3026 | 702 |
range := stop - start + 1. |
4600 | 703 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
704 |
bytesNeeded := (range highBit + 15) // 8. |
|
705 |
bytesNeeded < 4 ifTrue:[ |
|
706 |
rnd := (((self nextByte bitShift:8) + self nextByte) bitShift:8) + self nextByte. |
|
3134 | 707 |
] ifFalse:[ |
4600 | 708 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
3134 | 709 |
]. |
5201 | 710 |
rnd := rnd \\ range. |
4600 | 711 |
^ rnd + start |
712 |
||
713 |
"shows pair-combination counts |
|
714 |
||
715 |
|r v prev this counts| |
|
716 |
||
717 |
v := TextView new . |
|
718 |
v extent:500@250. |
|
719 |
v list:#('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 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx' |
|
728 |
'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'). |
|
729 |
v openAndWait. |
|
730 |
counts := (1 to:10) collect:[:row | Array new:10 withAll:0]. |
|
731 |
||
732 |
r := self new. |
|
733 |
prev := r nextIntegerBetween:1 and:10. |
|
734 |
10000 timesRepeat:[ |
|
735 |
100 timesRepeat:[ |
|
736 |
this := r nextIntegerBetween:1 and:10. |
|
737 |
(counts at:this) at:prev put:((counts at:this) at:prev)+1. |
|
738 |
]. |
|
739 |
v list:(counts collect:[:row | |
|
740 |
'%4d %4d %4d %4d %4d %4d %4d %4d %4d %4d'printf:row ]). |
|
741 |
prev := this. |
|
742 |
]. |
|
743 |
" |
|
17 | 744 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
745 |
" |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
746 |
|r| |
3026 | 747 |
r := self new. |
350 | 748 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
749 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
750 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
751 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 752 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
753 |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
754 |
" |
3134 | 755 |
|r| |
756 |
r := self new. |
|
757 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
758 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
759 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
760 |
Transcript showCR:(r nextIntegerBetween:1 and:1000000). |
|
761 |
" |
|
762 |
||
763 |
" |
|
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
764 |
|r bag| |
3026 | 765 |
r := self new. |
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
766 |
bag := Bag new. |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
767 |
1000000 timesRepeat:[ |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
768 |
bag add:(r nextIntegerBetween:-1 and:1). |
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
769 |
]. |
3134 | 770 |
Transcript showCR:bag sortedCounts. |
3026 | 771 |
" |
772 |
||
773 |
" |
|
774 |
|r bag| |
|
775 |
r := self new. |
|
776 |
bag := Bag new. |
|
777 |
1000000 timesRepeat:[ |
|
778 |
bag add:(r nextIntegerBetween:1 and:3). |
|
779 |
]. |
|
3134 | 780 |
Transcript showCR:bag sortedCounts. |
3026 | 781 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
782 |
" |
|
783 |
||
784 |
" |
|
785 |
|r bag| |
|
786 |
r := self new. |
|
787 |
bag := Bag new. |
|
788 |
1000000 timesRepeat:[ |
|
3134 | 789 |
bag add:(r nextIntegerBetween:1 and:32). |
3026 | 790 |
]. |
3134 | 791 |
Transcript showCR:bag sortedCounts. |
792 |
TestCase assert:(bag standardDeviation closeTo:(((32 squared - 1)/12) sqrt)). |
|
793 |
" |
|
794 |
||
680
a7b98fdac3dc
fixed nextIntegerBetween:and:
Claus Gittinger <cg@exept.de>
parents:
561
diff
changeset
|
795 |
" |
3134 | 796 |
|r bag| |
797 |
r := self new. |
|
798 |
bag := Bag new. |
|
799 |
100000000 timesRepeat:[ |
|
800 |
bag add:(r nextIntegerBetween:1 and:400000). |
|
801 |
]. |
|
802 |
Transcript showCR:bag sortedCounts. |
|
803 |
TestCase assert:(bag standardDeviation closeTo:(((400000 squared - 1)/12) sqrt)). |
|
804 |
" |
|
805 |
||
3675 | 806 |
" |
807 |
|r| |
|
808 |
||
809 |
r := self new. |
|
810 |
100000000 timesRepeat:[ |
|
811 |
self assert:((r nextIntegerBetween:1 and:3) between:1 and:3). |
|
812 |
]. |
|
813 |
" |
|
3750 | 814 |
! |
815 |
||
816 |
nextLettersOrDigits:count |
|
4600 | 817 |
"get a random string consisting of count printable letters or digits [0-9A-Za-z]." |
3750 | 818 |
|
819 |
|res cnt "{ Class:SmallInteger }"| |
|
820 |
||
821 |
cnt := count. |
|
822 |
res := String uninitializedNew:cnt. |
|
823 |
||
824 |
1 to:cnt do:[:i| |
|
825 |
self step. |
|
826 |
res |
|
827 |
at:i |
|
828 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:(seed \\ 62 + 1)). |
|
829 |
]. |
|
830 |
||
831 |
^ res |
|
832 |
||
833 |
" |
|
4600 | 834 |
Random new nextLettersOrDigits:40 |
3750 | 835 |
" |
0 | 836 |
! ! |
837 |
||
65 | 838 |
!Random methodsFor:'blocked methods'! |
0 | 839 |
|
65 | 840 |
contents |
841 |
"blocked from use - contents makes no sense for random generators" |
|
842 |
||
843 |
self shouldNotImplement |
|
844 |
! |
|
845 |
||
846 |
nextPut: value |
|
847 |
"blocked from use - it makes no sense for randoms" |
|
848 |
||
849 |
self shouldNotImplement |
|
850 |
! ! |
|
851 |
||
2459 | 852 |
!Random methodsFor:'initialization'! |
853 |
||
854 |
initialize |
|
855 |
self setSeed |
|
856 |
! ! |
|
857 |
||
65 | 858 |
!Random methodsFor:'private'! |
0 | 859 |
|
2430 | 860 |
addEntropy:entropyBytes |
861 |
"add some entropy - ignored here" |
|
862 |
||
863 |
^ self |
|
864 |
! |
|
865 |
||
2291 | 866 |
seed |
867 |
^ seed |
|
868 |
! |
|
869 |
||
0 | 870 |
setSeed |
3134 | 871 |
"set the initial seed value based on the current time and processId. |
872 |
These numbers implement a maximum period generator which passes |
|
873 |
the spectral test for randomness for dimensions 2 3 4 5 6 and |
|
874 |
the product does not overflow 2 raisedTo:29. |
|
875 |
||
876 |
Use both time and processId for seed, to make different processes |
|
877 |
return different Random numbers" |
|
878 |
||
5475 | 879 |
self setSeed:(self class randomSeed bitAnd:16rFFFFFFFF). |
3134 | 880 |
|
881 |
"Modified: / 29-05-2007 / 12:07:37 / cg" |
|
5475 | 882 |
"Modified: / 16-04-2020 / 17:11:19 / Stefan Vogel" |
3134 | 883 |
! |
884 |
||
885 |
setSeed:seedValue |
|
4600 | 886 |
"set the initial seed and intialize the PRNG parameters." |
842 | 887 |
|
4600 | 888 |
self subclassResponsibility. |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
889 |
|
1878 | 890 |
"Modified: / 12-11-1999 / 17:50:52 / stefan" |
891 |
"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
|
892 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
893 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
894 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
895 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
896 |
|
4600 | 897 |
self subclassResponsibility |
0 | 898 |
! ! |
65 | 899 |
|
5250 | 900 |
!Random methodsFor:'reading'! |
901 |
||
902 |
next |
|
903 |
"return the next random number in the range ]0..1[" |
|
904 |
||
905 |
self subclassResponsibility. |
|
906 |
||
907 |
" |
|
908 |
|r| |
|
909 |
r := Random new. |
|
910 |
Transcript showCR:r next. |
|
911 |
Transcript showCR:r next. |
|
912 |
Transcript showCR:r next. |
|
913 |
Transcript showCR:r next. |
|
914 |
" |
|
915 |
||
916 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
|
917 |
! ! |
|
918 |
||
142 | 919 |
!Random methodsFor:'testing'! |
920 |
||
921 |
atEnd |
|
922 |
"instances of Random can always give more numbers" |
|
923 |
||
924 |
^ false |
|
925 |
! |
|
926 |
||
927 |
isReadable |
|
928 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
929 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
930 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
931 |
isWritable |
3781 | 932 |
"return true, if writing is supported by the receiver. |
933 |
Random Generators never are" |
|
934 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
935 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
936 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
937 |
"Created: 1.4.1997 / 22:38:27 / cg" |
4600 | 938 |
! |
939 |
||
940 |
maxInteger |
|
941 |
"the max value returned by self nextInteger" |
|
942 |
||
943 |
self subclassResponsibility |
|
142 | 944 |
! ! |
945 |
||
946 |
!Random class methodsFor:'documentation'! |
|
947 |
||
948 |
version |
|
3675 | 949 |
^ '$Header$' |
2291 | 950 |
! |
951 |
||
952 |
version_CVS |
|
3675 | 953 |
^ '$Header$' |
142 | 954 |
! ! |
3026 | 955 |