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