author | Claus Gittinger <cg@exept.de> |
Mon, 06 Mar 2017 18:16:14 +0100 | |
changeset 4357 | 4d4abde7a96a |
parent 4250 | c21944d9e5d7 |
child 4482 | 6a22682659e2 |
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:'' |
4250 | 18 |
classVariableNames:'HasOSRandom RandFile RandomDevicePathes SharedRandomGenerator' |
3854 | 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. |
|
4250 | 100 |
Try to get system random numbers from OS (e.g. in LINUX) |
101 |
or device urandom (e.g. in Linux, OSX). |
|
4025 | 102 |
If no system random numbers are available, fall back to |
4250 | 103 |
a cryptographic secure PRNG (like RC4Random, part of the extra libcrypt package). |
1606 | 104 |
As last resort fallback to the cryptographic insecure linear builtin PRNG" |
105 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
106 |
|result| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
107 |
|
3193 | 108 |
SharedRandomGenerator notNil ifTrue:[ |
4250 | 109 |
"each time, we do a new, add some entropy to the SharedGenerator" |
3193 | 110 |
SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime. |
111 |
^ SharedRandomGenerator. |
|
2432 | 112 |
]. |
113 |
||
3854 | 114 |
HasOSRandom isNil ifTrue:[ |
3777 | 115 |
"/ see if there is an OS or CPU-random generator |
116 |
[ |
|
117 |
"fetch a random byte - and check if the OS generator works" |
|
118 |
result := OperatingSystem randomBytesInto:1. |
|
119 |
] on:PrimitiveFailure do:[:ex| ]. |
|
120 |
HasOSRandom := result notNil. |
|
121 |
]. |
|
4025 | 122 |
|
3777 | 123 |
HasOSRandom ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
124 |
"OperatingSystem knows how to get random bytes" |
3193 | 125 |
SharedRandomGenerator := self basicNew. |
3203 | 126 |
^ SharedRandomGenerator. |
3777 | 127 |
]. |
4025 | 128 |
|
3777 | 129 |
RandFile isNil ifTrue:[ |
130 |
self openRandFile. |
|
131 |
]. |
|
132 |
RandFile notNil ifTrue:[ |
|
133 |
SharedRandomGenerator := self basicNew. |
|
134 |
^ SharedRandomGenerator. |
|
1606 | 135 |
]. |
136 |
||
4025 | 137 |
"/ fallback |
2432 | 138 |
Rc4Cipher notNil ifTrue:[ |
3193 | 139 |
SharedRandomGenerator := Rc4Cipher random. |
140 |
^ SharedRandomGenerator. |
|
2432 | 141 |
]. |
142 |
||
4025 | 143 |
"/ last fallback - not very good |
3193 | 144 |
SharedRandomGenerator := Random sharedGenerator. |
145 |
^ SharedRandomGenerator. |
|
1606 | 146 |
! ! |
147 |
||
2432 | 148 |
!RandomGenerator class methodsFor:'adding entropy'! |
149 |
||
150 |
addEntropy:entropyBytes |
|
3193 | 151 |
SharedRandomGenerator notNil ifTrue:[ |
152 |
SharedRandomGenerator addEntropy:entropyBytes |
|
2432 | 153 |
]. |
154 |
! ! |
|
155 |
||
1606 | 156 |
!RandomGenerator class methodsFor:'change & update'! |
157 |
||
158 |
update:something with:aParameter from:changedObject |
|
159 |
"handle image restarts and flush any device resource handles" |
|
160 |
||
3777 | 161 |
(changedObject == ObjectMemory) ifTrue:[ |
162 |
(something == #returnFromSnapshot) ifTrue:[ |
|
163 |
SharedRandomGenerator notNil ifTrue:[ |
|
164 |
SharedRandomGenerator := nil. |
|
165 |
HasOSRandom := RandFile := nil. |
|
166 |
self new. "/ opens a new sharedGenerator |
|
167 |
] |
|
168 |
] |
|
1606 | 169 |
]. |
170 |
! ! |
|
171 |
||
172 |
!RandomGenerator class methodsFor:'queries'! |
|
173 |
||
174 |
randPath |
|
175 |
"path to a file/device that is a source or random numbers" |
|
176 |
||
4041 | 177 |
|pathsOrNil| |
3777 | 178 |
|
4041 | 179 |
pathsOrNil := self randomDevicePathes. |
180 |
pathsOrNil notNil ifTrue:[ |
|
181 |
pathsOrNil do:[:triedRandom | |
|
3777 | 182 |
triedRandom asFilename exists ifTrue:[ |
183 |
^ triedRandom |
|
184 |
] |
|
4041 | 185 |
]. |
3777 | 186 |
]. |
1606 | 187 |
^ nil. |
3777 | 188 |
! |
189 |
||
190 |
randomDevicePathes |
|
4250 | 191 |
"paths to look for OS sources of random numbers. |
192 |
Can be configured at early startup to use something special |
|
193 |
(i.e. a special hardware random device) |
|
194 |
If never set, the defaults /dev/random, /dev/urandom are used" |
|
3777 | 195 |
|
196 |
RandomDevicePathes notNil ifTrue:[^ RandomDevicePathes]. |
|
197 |
OperatingSystem isUNIXlike ifTrue:[ |
|
198 |
^ #( '/dev/urandom' '/dev/random' ) |
|
199 |
]. |
|
200 |
^ nil. |
|
201 |
! |
|
202 |
||
4250 | 203 |
randomDevicePathes:aCollectionOfPathesOrNil |
4041 | 204 |
"configurable paths to look for OS sources of random numbers. |
4250 | 205 |
(can be set during early startup in an rc-file). |
206 |
If never set, the defaults /dev/random, /dev/urandom are used" |
|
3777 | 207 |
|
4250 | 208 |
RandomDevicePathes := aCollectionOfPathesOrNil. |
1606 | 209 |
! ! |
210 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
211 |
!RandomGenerator methodsFor:'basic reading'! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
212 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
213 |
nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
214 |
"get the next random byte" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
215 |
|
3777 | 216 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
217 |
^ OperatingSystem randomBytesInto:1. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
218 |
]. |
3632 | 219 |
RandFile isOpen ifFalse:[ |
220 |
self class openRandFile. |
|
221 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
222 |
^ RandFile nextByte |
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 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
225 |
RandomGenerator new nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
226 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
227 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
228 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
229 |
Distribution should be equal: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
230 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
231 |
|r bag| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
232 |
r := RandomGenerator new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
233 |
bag := Bag new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
234 |
1000000 timesRepeat:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
235 |
bag add:(r nextByte). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
236 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
237 |
bag. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
238 |
Transcript showCR:bag contents |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
239 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
240 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
241 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
242 |
! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
243 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
244 |
nextBytes:cnt |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
245 |
"get the next cnt random bytes" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
246 |
|
3777 | 247 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
248 |
^ OperatingSystem randomBytesInto:(ByteArray new:cnt). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
249 |
]. |
3632 | 250 |
RandFile isOpen ifFalse:[ |
251 |
self class openRandFile. |
|
252 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
253 |
^ RandFile nextBytes:cnt. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
254 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
255 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
256 |
RandomGenerator new nextBytes:4 |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
257 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
258 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
259 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
260 |
"Modified: / 11.11.1999 / 09:52:26 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
261 |
! ! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
262 |
|
1606 | 263 |
!RandomGenerator methodsFor:'reading'! |
264 |
||
265 |
next |
|
266 |
"return the next random number in the range 0..1" |
|
267 |
||
3751 | 268 |
^ self nextInteger / Float maxSmallInteger |
1606 | 269 |
|
270 |
" |
|
271 |
|r| |
|
272 |
r := RandomGenerator new. |
|
273 |
Transcript showCR:r next. |
|
274 |
Transcript showCR:r next. |
|
275 |
Transcript showCR:r next. |
|
276 |
Transcript showCR:r next. |
|
277 |
" |
|
278 |
||
279 |
"Modified: / 11.11.1999 / 10:31:35 / stefan" |
|
280 |
! |
|
281 |
||
282 |
nextBoolean |
|
283 |
"return true or false by random" |
|
284 |
||
285 |
^ self nextByte <= 127 |
|
286 |
||
287 |
" |
|
288 |
|r| |
|
289 |
r := RandomGenerator new. |
|
290 |
Transcript showCR:r nextBoolean. |
|
291 |
Transcript showCR:r nextBoolean. |
|
292 |
Transcript showCR:r nextBoolean. |
|
293 |
Transcript showCR:r nextBoolean. |
|
294 |
" |
|
295 |
||
296 |
" |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
297 |
Distribution should approach 50/50: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
298 |
|
1606 | 299 |
|r bag| |
300 |
r := RandomGenerator new. |
|
301 |
bag := Bag new. |
|
302 |
1000000 timesRepeat:[ |
|
303 |
bag add:(r nextBoolean). |
|
304 |
]. |
|
305 |
Transcript showCR:bag contents |
|
306 |
" |
|
307 |
||
308 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
309 |
"Modified: / 12.11.1999 / 17:22:01 / stefan" |
|
310 |
! |
|
311 |
||
3751 | 312 |
nextCharacters:count |
313 |
"get the next count printable characters. |
|
314 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
|
1640 | 315 |
|
3751 | 316 |
|bytes string cnt "{ Class:SmallInteger }"| |
1640 | 317 |
|
3751 | 318 |
cnt := count. |
1640 | 319 |
bytes := self nextBytes:cnt. |
320 |
string := String new:cnt. |
|
321 |
||
3751 | 322 |
1 to:cnt do:[:eachIndex| |
323 |
string at:eachIndex put:(Character value:((bytes at:eachIndex) \\ 94 + 32)). |
|
1640 | 324 |
]. |
325 |
||
326 |
^ string |
|
327 |
||
328 |
" |
|
329 |
RandomGenerator new nextCharacters:8 |
|
330 |
" |
|
331 |
! |
|
332 |
||
3632 | 333 |
nextInteger |
334 |
"return the next integral random number, |
|
3751 | 335 |
in the range 0 .. 16r3FFFFFFF (32-bit) |
336 |
or 0 .. 16r3FFFFFFFFFFFFFFF (64-bit)." |
|
3632 | 337 |
|
3751 | 338 |
|res intSize| |
339 |
||
340 |
intSize := SmallInteger maxBytes. |
|
3632 | 341 |
|
3777 | 342 |
HasOSRandom == true ifTrue:[ |
3751 | 343 |
^ OperatingSystem randomBytesInto:intSize. |
3632 | 344 |
]. |
345 |
||
3751 | 346 |
res := self nextBytes:intSize. |
347 |
res at:intSize put:((res at:intSize) bitAnd:16r3F). |
|
348 |
^ res asIntegerMSB:false. |
|
3632 | 349 |
|
350 |
||
351 |
" |
|
352 |
|r| |
|
353 |
r := RandomGenerator new. |
|
354 |
Transcript showCR:r nextInteger. |
|
355 |
Transcript showCR:r nextInteger. |
|
356 |
Transcript showCR:r nextInteger. |
|
357 |
Transcript showCR:r nextInteger. |
|
358 |
" |
|
359 |
||
360 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
361 |
! |
|
362 |
||
1606 | 363 |
nextIntegerBetween:start and:stop |
364 |
"return an integral random number between start and stop" |
|
365 |
||
3025 | 366 |
|rnd range bytesNeeded| |
1606 | 367 |
|
3025 | 368 |
range := stop - start + 1. |
3133 | 369 |
bytesNeeded := (range highBit + 15) // 8. |
3025 | 370 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
371 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
|
372 |
rnd := rnd \\ range. |
|
373 |
^ rnd + start |
|
1606 | 374 |
|
375 |
" |
|
376 |
|r| |
|
3025 | 377 |
r := self new. |
1606 | 378 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
379 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
380 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
381 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
382 |
" |
|
383 |
||
384 |
" |
|
385 |
|r bag| |
|
3025 | 386 |
r := self new. |
1606 | 387 |
bag := Bag new. |
388 |
1000000 timesRepeat:[ |
|
389 |
bag add:(r nextIntegerBetween:-1 and:1). |
|
390 |
]. |
|
3133 | 391 |
Transcript showCR:bag sortedCounts. |
3025 | 392 |
" |
393 |
||
394 |
" |
|
395 |
|r bag| |
|
396 |
r := self new. |
|
397 |
bag := Bag new. |
|
398 |
1000000 timesRepeat:[ |
|
399 |
bag add:(r nextIntegerBetween:1 and:3). |
|
400 |
]. |
|
3133 | 401 |
Transcript showCR:bag sortedCounts. |
3025 | 402 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
403 |
" |
|
404 |
||
405 |
" |
|
406 |
|r bag| |
|
407 |
r := self new. |
|
408 |
bag := Bag new. |
|
409 |
1000000 timesRepeat:[ |
|
410 |
bag add:(r nextIntegerBetween:1 and:10). |
|
411 |
]. |
|
3133 | 412 |
Transcript showCR:bag sortedCounts. |
3025 | 413 |
TestCase assert:(bag standardDeviation closeTo:(((10 squared - 1)/12) sqrt)). |
1606 | 414 |
" |
415 |
||
416 |
"Created: / 11.11.1999 / 10:28:36 / stefan" |
|
3751 | 417 |
! |
418 |
||
419 |
nextLettersOrDigits:count |
|
420 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
421 |
||
422 |
|bytes string cnt "{ Class:SmallInteger }"| |
|
423 |
||
424 |
cnt := count. |
|
425 |
bytes := self nextBytes:cnt. |
|
426 |
string := String new:cnt. |
|
427 |
||
428 |
1 to:cnt do:[:eachIndex| |
|
429 |
string |
|
430 |
at:eachIndex |
|
431 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:((bytes at:eachIndex) \\ 62 + 1)). |
|
432 |
]. |
|
433 |
||
434 |
^ string |
|
435 |
||
436 |
" |
|
437 |
RandomGenerator new nextLettersOrDigits:40 |
|
438 |
" |
|
1606 | 439 |
! ! |
440 |
||
441 |
!RandomGenerator methodsFor:'writing'! |
|
442 |
||
443 |
nextPut:something |
|
444 |
"change the random pool by feeding in something. |
|
445 |
Something should be some unpredictable, random event. |
|
446 |
Ignored here" |
|
447 |
||
448 |
||
449 |
! |
|
450 |
||
451 |
nextPutAll:something |
|
452 |
"change the random pool by feeding in something. |
|
453 |
Something should be some unpredictable, random event. |
|
454 |
Ignored here" |
|
455 |
||
456 |
! ! |
|
457 |
||
458 |
!RandomGenerator class methodsFor:'documentation'! |
|
459 |
||
460 |
version |
|
3632 | 461 |
^ '$Header$' |
2432 | 462 |
! |
463 |
||
464 |
version_CVS |
|
3632 | 465 |
^ '$Header$' |
1606 | 466 |
! ! |
467 |
||
3025 | 468 |
|
1606 | 469 |
RandomGenerator initialize! |