author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:36:55 +0200 | |
changeset 3898 | c90424dba938 |
parent 3854 | 746533c3d2ae |
child 4025 | 7d6791051cb5 |
permissions | -rw-r--r-- |
1925 | 1 |
" |
2 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
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. |
|
11 |
" |
|
1606 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3632 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3193 | 16 |
Random subclass:#RandomGenerator |
3854 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'HasOSRandom RandFile SharedRandomGenerator RandomDevicePathes' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Magnitude-Numbers' |
|
1606 | 21 |
! |
22 |
||
23 |
!RandomGenerator class methodsFor:'documentation'! |
|
24 |
||
1925 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
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. |
|
36 |
" |
|
37 |
! |
|
38 |
||
1606 | 39 |
documentation |
40 |
" |
|
3777 | 41 |
This is a Random number generator, |
42 |
which uses either a OS random number generator or /dev/urandom, |
|
43 |
or an ST/X internal random number generator. |
|
44 |
||
3351
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
45 |
Warning: this generator should not be used for cryptographic work |
3777 | 46 |
UNLESS: |
3351
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
47 |
1) you are running on linux, solaris or osx, |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
48 |
2) you have a working /dev/urandom. |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
49 |
3) you can trust your /dev/urandom (I don't really know if any/all of them are good) |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
50 |
|
1606 | 51 |
[author:] |
52 |
Stefan Vogel |
|
53 |
||
54 |
[see also:] |
|
3391 | 55 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf |
1606 | 56 |
Random HashRandom Rc4Stream |
57 |
||
58 |
[instance variables:] |
|
59 |
||
60 |
[class variables:] |
|
3777 | 61 |
HasOSRandom true if the operatingSystem supports random numbers |
62 |
RandFile (if HasOSRandom is false and non-nil) the FileStream (/dev/random), |
|
63 |
we get random numbers from |
|
1606 | 64 |
" |
65 |
! ! |
|
66 |
||
67 |
!RandomGenerator class methodsFor:'initialization'! |
|
68 |
||
69 |
initialize |
|
70 |
"want to be informed when returning from snapshot" |
|
71 |
||
72 |
ObjectMemory addDependent:self. |
|
73 |
||
74 |
||
75 |
! |
|
76 |
||
77 |
openRandFile |
|
78 |
"try to open a random device" |
|
79 |
||
80 |
|randDevName| |
|
81 |
||
3777 | 82 |
RandFile notNil ifTrue:[ |
1606 | 83 |
RandFile close. |
3777 | 84 |
RandFile := nil. |
1606 | 85 |
]. |
86 |
||
87 |
randDevName := self randPath. |
|
88 |
randDevName notNil ifTrue:[ |
|
89 |
randDevName := randDevName asFilename. |
|
90 |
randDevName isReadable ifTrue:[ |
|
3777 | 91 |
RandFile := randDevName readStream. |
1606 | 92 |
]. |
93 |
]. |
|
94 |
! ! |
|
95 |
||
96 |
!RandomGenerator class methodsFor:'instance creation'! |
|
97 |
||
98 |
new |
|
99 |
"return a new random number generator. |
|
100 |
Try to get system random numbers from device (e.g. in LINUX). |
|
101 |
If no system random nubers are available, fall back to |
|
102 |
a cryptographic secure PRNG (part of the extra libcrypt package). |
|
103 |
As last resort fallback to the cryptographic insecure linear builtin PRNG" |
|
104 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
105 |
|result| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
106 |
|
3193 | 107 |
SharedRandomGenerator notNil ifTrue:[ |
2432 | 108 |
"each time, we do an new, add some entropy to the SharedGenerator" |
3193 | 109 |
SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime. |
110 |
^ SharedRandomGenerator. |
|
2432 | 111 |
]. |
112 |
||
3854 | 113 |
HasOSRandom isNil ifTrue:[ |
3777 | 114 |
"/ see if there is an OS or CPU-random generator |
115 |
[ |
|
116 |
"fetch a random byte - and check if the OS generator works" |
|
117 |
result := OperatingSystem randomBytesInto:1. |
|
118 |
] on:PrimitiveFailure do:[:ex| ]. |
|
119 |
HasOSRandom := result notNil. |
|
120 |
]. |
|
121 |
||
122 |
HasOSRandom ifTrue:[ |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
123 |
"OperatingSystem knows how to get random bytes" |
3193 | 124 |
SharedRandomGenerator := self basicNew. |
3203 | 125 |
^ SharedRandomGenerator. |
3777 | 126 |
]. |
127 |
||
128 |
RandFile isNil ifTrue:[ |
|
129 |
self openRandFile. |
|
130 |
]. |
|
131 |
RandFile notNil ifTrue:[ |
|
132 |
SharedRandomGenerator := self basicNew. |
|
133 |
^ SharedRandomGenerator. |
|
1606 | 134 |
]. |
135 |
||
3777 | 136 |
"/ fallback |
2432 | 137 |
Rc4Cipher notNil ifTrue:[ |
3193 | 138 |
SharedRandomGenerator := Rc4Cipher random. |
139 |
^ SharedRandomGenerator. |
|
2432 | 140 |
]. |
141 |
||
3777 | 142 |
"/ last fallback - not very good |
3193 | 143 |
SharedRandomGenerator := Random sharedGenerator. |
144 |
^ SharedRandomGenerator. |
|
1606 | 145 |
! ! |
146 |
||
2432 | 147 |
!RandomGenerator class methodsFor:'adding entropy'! |
148 |
||
149 |
addEntropy:entropyBytes |
|
3193 | 150 |
SharedRandomGenerator notNil ifTrue:[ |
151 |
SharedRandomGenerator addEntropy:entropyBytes |
|
2432 | 152 |
]. |
153 |
! ! |
|
154 |
||
1606 | 155 |
!RandomGenerator class methodsFor:'change & update'! |
156 |
||
157 |
update:something with:aParameter from:changedObject |
|
158 |
"handle image restarts and flush any device resource handles" |
|
159 |
||
3777 | 160 |
(changedObject == ObjectMemory) ifTrue:[ |
161 |
(something == #returnFromSnapshot) ifTrue:[ |
|
162 |
SharedRandomGenerator notNil ifTrue:[ |
|
163 |
SharedRandomGenerator := nil. |
|
164 |
HasOSRandom := RandFile := nil. |
|
165 |
self new. "/ opens a new sharedGenerator |
|
166 |
] |
|
167 |
] |
|
1606 | 168 |
]. |
169 |
! ! |
|
170 |
||
171 |
!RandomGenerator class methodsFor:'queries'! |
|
172 |
||
173 |
randPath |
|
174 |
"path to a file/device that is a source or random numbers" |
|
175 |
||
3777 | 176 |
|pathesOrNil| |
177 |
||
178 |
pathesOrNil := self randomDevicePathes. |
|
179 |
pathesOrNil notNil ifTrue:[ |
|
180 |
pathesOrNil do:[:triedRandom | |
|
181 |
triedRandom asFilename exists ifTrue:[ |
|
182 |
^ triedRandom |
|
183 |
] |
|
184 |
]. |
|
185 |
]. |
|
1606 | 186 |
^ nil. |
3777 | 187 |
! |
188 |
||
189 |
randomDevicePathes |
|
190 |
"pathes to look for OS sources of random numbers" |
|
191 |
||
192 |
RandomDevicePathes notNil ifTrue:[^ RandomDevicePathes]. |
|
193 |
OperatingSystem isUNIXlike ifTrue:[ |
|
194 |
^ #( '/dev/urandom' '/dev/random' ) |
|
195 |
]. |
|
196 |
^ nil. |
|
197 |
! |
|
198 |
||
199 |
randomDevicePathes:aCollectionOfpathesOrNil |
|
200 |
"configurable pathes to look for OS sources of random numbers. |
|
201 |
(can be set during early startup in an rc-file)" |
|
202 |
||
203 |
RandomDevicePathes := aCollectionOfpathesOrNil. |
|
1606 | 204 |
! ! |
205 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
206 |
!RandomGenerator methodsFor:'basic reading'! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
207 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
208 |
nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
209 |
"get the next random byte" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
210 |
|
3777 | 211 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
212 |
^ OperatingSystem randomBytesInto:1. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
213 |
]. |
3632 | 214 |
RandFile isOpen ifFalse:[ |
215 |
self class openRandFile. |
|
216 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
217 |
^ RandFile nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
218 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
219 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
220 |
RandomGenerator new nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
221 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
222 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
223 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
224 |
Distribution should be equal: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
225 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
226 |
|r bag| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
227 |
r := RandomGenerator new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
228 |
bag := Bag new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
229 |
1000000 timesRepeat:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
230 |
bag add:(r nextByte). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
231 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
232 |
bag. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
233 |
Transcript showCR:bag contents |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
234 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
235 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
236 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
237 |
! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
238 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
239 |
nextBytes:cnt |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
240 |
"get the next cnt random bytes" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
241 |
|
3777 | 242 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
243 |
^ OperatingSystem randomBytesInto:(ByteArray new:cnt). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
244 |
]. |
3632 | 245 |
RandFile isOpen ifFalse:[ |
246 |
self class openRandFile. |
|
247 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
248 |
^ RandFile nextBytes:cnt. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
249 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
250 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
251 |
RandomGenerator new nextBytes:4 |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
252 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
253 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
254 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
255 |
"Modified: / 11.11.1999 / 09:52:26 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
256 |
! ! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
257 |
|
1606 | 258 |
!RandomGenerator methodsFor:'reading'! |
259 |
||
260 |
next |
|
261 |
"return the next random number in the range 0..1" |
|
262 |
||
3751 | 263 |
^ self nextInteger / Float maxSmallInteger |
1606 | 264 |
|
265 |
" |
|
266 |
|r| |
|
267 |
r := RandomGenerator new. |
|
268 |
Transcript showCR:r next. |
|
269 |
Transcript showCR:r next. |
|
270 |
Transcript showCR:r next. |
|
271 |
Transcript showCR:r next. |
|
272 |
" |
|
273 |
||
274 |
"Modified: / 11.11.1999 / 10:31:35 / stefan" |
|
275 |
! |
|
276 |
||
277 |
nextBoolean |
|
278 |
"return true or false by random" |
|
279 |
||
280 |
^ self nextByte <= 127 |
|
281 |
||
282 |
" |
|
283 |
|r| |
|
284 |
r := RandomGenerator new. |
|
285 |
Transcript showCR:r nextBoolean. |
|
286 |
Transcript showCR:r nextBoolean. |
|
287 |
Transcript showCR:r nextBoolean. |
|
288 |
Transcript showCR:r nextBoolean. |
|
289 |
" |
|
290 |
||
291 |
" |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
292 |
Distribution should approach 50/50: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
293 |
|
1606 | 294 |
|r bag| |
295 |
r := RandomGenerator new. |
|
296 |
bag := Bag new. |
|
297 |
1000000 timesRepeat:[ |
|
298 |
bag add:(r nextBoolean). |
|
299 |
]. |
|
300 |
Transcript showCR:bag contents |
|
301 |
" |
|
302 |
||
303 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
304 |
"Modified: / 12.11.1999 / 17:22:01 / stefan" |
|
305 |
! |
|
306 |
||
3751 | 307 |
nextCharacters:count |
308 |
"get the next count printable characters. |
|
309 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
|
1640 | 310 |
|
3751 | 311 |
|bytes string cnt "{ Class:SmallInteger }"| |
1640 | 312 |
|
3751 | 313 |
cnt := count. |
1640 | 314 |
bytes := self nextBytes:cnt. |
315 |
string := String new:cnt. |
|
316 |
||
3751 | 317 |
1 to:cnt do:[:eachIndex| |
318 |
string at:eachIndex put:(Character value:((bytes at:eachIndex) \\ 94 + 32)). |
|
1640 | 319 |
]. |
320 |
||
321 |
^ string |
|
322 |
||
323 |
" |
|
324 |
RandomGenerator new nextCharacters:8 |
|
325 |
" |
|
326 |
! |
|
327 |
||
3632 | 328 |
nextInteger |
329 |
"return the next integral random number, |
|
3751 | 330 |
in the range 0 .. 16r3FFFFFFF (32-bit) |
331 |
or 0 .. 16r3FFFFFFFFFFFFFFF (64-bit)." |
|
3632 | 332 |
|
3751 | 333 |
|res intSize| |
334 |
||
335 |
intSize := SmallInteger maxBytes. |
|
3632 | 336 |
|
3777 | 337 |
HasOSRandom == true ifTrue:[ |
3751 | 338 |
^ OperatingSystem randomBytesInto:intSize. |
3632 | 339 |
]. |
340 |
||
3751 | 341 |
res := self nextBytes:intSize. |
342 |
res at:intSize put:((res at:intSize) bitAnd:16r3F). |
|
343 |
^ res asIntegerMSB:false. |
|
3632 | 344 |
|
345 |
||
346 |
" |
|
347 |
|r| |
|
348 |
r := RandomGenerator new. |
|
349 |
Transcript showCR:r nextInteger. |
|
350 |
Transcript showCR:r nextInteger. |
|
351 |
Transcript showCR:r nextInteger. |
|
352 |
Transcript showCR:r nextInteger. |
|
353 |
" |
|
354 |
||
355 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
356 |
! |
|
357 |
||
1606 | 358 |
nextIntegerBetween:start and:stop |
359 |
"return an integral random number between start and stop" |
|
360 |
||
3025 | 361 |
|rnd range bytesNeeded| |
1606 | 362 |
|
3025 | 363 |
range := stop - start + 1. |
3133 | 364 |
bytesNeeded := (range highBit + 15) // 8. |
3025 | 365 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
366 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
|
367 |
rnd := rnd \\ range. |
|
368 |
^ rnd + start |
|
1606 | 369 |
|
370 |
" |
|
371 |
|r| |
|
3025 | 372 |
r := self new. |
1606 | 373 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
374 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
375 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
376 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
377 |
" |
|
378 |
||
379 |
" |
|
380 |
|r bag| |
|
3025 | 381 |
r := self new. |
1606 | 382 |
bag := Bag new. |
383 |
1000000 timesRepeat:[ |
|
384 |
bag add:(r nextIntegerBetween:-1 and:1). |
|
385 |
]. |
|
3133 | 386 |
Transcript showCR:bag sortedCounts. |
3025 | 387 |
" |
388 |
||
389 |
" |
|
390 |
|r bag| |
|
391 |
r := self new. |
|
392 |
bag := Bag new. |
|
393 |
1000000 timesRepeat:[ |
|
394 |
bag add:(r nextIntegerBetween:1 and:3). |
|
395 |
]. |
|
3133 | 396 |
Transcript showCR:bag sortedCounts. |
3025 | 397 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
398 |
" |
|
399 |
||
400 |
" |
|
401 |
|r bag| |
|
402 |
r := self new. |
|
403 |
bag := Bag new. |
|
404 |
1000000 timesRepeat:[ |
|
405 |
bag add:(r nextIntegerBetween:1 and:10). |
|
406 |
]. |
|
3133 | 407 |
Transcript showCR:bag sortedCounts. |
3025 | 408 |
TestCase assert:(bag standardDeviation closeTo:(((10 squared - 1)/12) sqrt)). |
1606 | 409 |
" |
410 |
||
411 |
"Created: / 11.11.1999 / 10:28:36 / stefan" |
|
3751 | 412 |
! |
413 |
||
414 |
nextLettersOrDigits:count |
|
415 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
416 |
||
417 |
|bytes string cnt "{ Class:SmallInteger }"| |
|
418 |
||
419 |
cnt := count. |
|
420 |
bytes := self nextBytes:cnt. |
|
421 |
string := String new:cnt. |
|
422 |
||
423 |
1 to:cnt do:[:eachIndex| |
|
424 |
string |
|
425 |
at:eachIndex |
|
426 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:((bytes at:eachIndex) \\ 62 + 1)). |
|
427 |
]. |
|
428 |
||
429 |
^ string |
|
430 |
||
431 |
" |
|
432 |
RandomGenerator new nextLettersOrDigits:40 |
|
433 |
" |
|
1606 | 434 |
! ! |
435 |
||
436 |
!RandomGenerator methodsFor:'writing'! |
|
437 |
||
438 |
nextPut:something |
|
439 |
"change the random pool by feeding in something. |
|
440 |
Something should be some unpredictable, random event. |
|
441 |
Ignored here" |
|
442 |
||
443 |
||
444 |
! |
|
445 |
||
446 |
nextPutAll:something |
|
447 |
"change the random pool by feeding in something. |
|
448 |
Something should be some unpredictable, random event. |
|
449 |
Ignored here" |
|
450 |
||
451 |
! ! |
|
452 |
||
453 |
!RandomGenerator class methodsFor:'documentation'! |
|
454 |
||
455 |
version |
|
3632 | 456 |
^ '$Header$' |
2432 | 457 |
! |
458 |
||
459 |
version_CVS |
|
3632 | 460 |
^ '$Header$' |
1606 | 461 |
! ! |
462 |
||
3025 | 463 |
|
1606 | 464 |
RandomGenerator initialize! |