author | Stefan Vogel <sv@exept.de> |
Thu, 01 Oct 2015 19:04:41 +0200 | |
changeset 3632 | 28303443fc03 |
parent 3391 | 2ca92dff09bb |
child 3751 | 56b00c8eea80 |
permissions | -rw-r--r-- |
3632 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1925 | 3 |
" |
4 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
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. |
|
13 |
" |
|
1606 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
3632 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
3193 | 18 |
Random subclass:#RandomGenerator |
1606 | 19 |
instanceVariableNames:'' |
3193 | 20 |
classVariableNames:'RandFile SharedRandomGenerator' |
1606 | 21 |
poolDictionaries:'' |
22 |
category:'Magnitude-Numbers' |
|
23 |
! |
|
24 |
||
25 |
!RandomGenerator class methodsFor:'documentation'! |
|
26 |
||
1925 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
30 |
All Rights Reserved |
|
31 |
||
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. |
|
38 |
" |
|
39 |
! |
|
40 |
||
1606 | 41 |
documentation |
42 |
" |
|
3351
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
43 |
Warning: this generator should not be used for cryptographic work |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
44 |
unless: |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
45 |
1) you are running on linux, solaris or osx, |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
46 |
2) you have a working /dev/urandom. |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
47 |
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
|
48 |
|
3339 | 49 |
This is a Random number generator, |
50 |
which uses either a OS random number generator (/dev/urandom), |
|
51 |
or an ST/X internal random number generator. |
|
1606 | 52 |
|
53 |
[author:] |
|
54 |
Stefan Vogel |
|
55 |
||
56 |
[see also:] |
|
3391 | 57 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf |
1606 | 58 |
Random HashRandom Rc4Stream |
59 |
||
60 |
[instance variables:] |
|
61 |
||
62 |
[class variables:] |
|
63 |
RandFile the FileStream we get random numbers from |
|
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 |
||
82 |
RandFile isStream ifTrue:[ |
|
83 |
RandFile close. |
|
84 |
]. |
|
85 |
||
86 |
RandFile := false. "prevent retry" |
|
87 |
||
88 |
randDevName := self randPath. |
|
89 |
randDevName notNil ifTrue:[ |
|
90 |
randDevName := randDevName asFilename. |
|
91 |
randDevName isReadable ifTrue:[ |
|
92 |
RandFile := randDevName readStream |
|
93 |
]. |
|
94 |
]. |
|
95 |
! ! |
|
96 |
||
97 |
!RandomGenerator class methodsFor:'instance creation'! |
|
98 |
||
99 |
new |
|
100 |
"return a new random number generator. |
|
101 |
Try to get system random numbers from device (e.g. in LINUX). |
|
102 |
If no system random nubers are available, fall back to |
|
103 |
a cryptographic secure PRNG (part of the extra libcrypt package). |
|
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:[ |
2432 | 109 |
"each time, we do an new, add some entropy to the SharedGenerator" |
3193 | 110 |
SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime. |
111 |
^ SharedRandomGenerator. |
|
2432 | 112 |
]. |
113 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
114 |
[ |
2440 | 115 |
"fetch a random byte - and check if the OS generator works" |
116 |
result := OperatingSystem randomBytesInto:1. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
117 |
] on:PrimitiveFailure do:[:ex| ]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
118 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
119 |
result notNil ifTrue:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
120 |
"OperatingSystem knows how to get random bytes" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
121 |
RandFile := true. |
3193 | 122 |
SharedRandomGenerator := self basicNew. |
3203 | 123 |
^ SharedRandomGenerator. |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
124 |
] ifFalse:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
125 |
RandFile isNil ifTrue:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
126 |
self openRandFile. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
127 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
128 |
RandFile isStream ifTrue:[ |
3193 | 129 |
SharedRandomGenerator := self basicNew. |
130 |
^ SharedRandomGenerator. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
131 |
]. |
1606 | 132 |
]. |
133 |
||
2432 | 134 |
Rc4Cipher notNil ifTrue:[ |
3193 | 135 |
SharedRandomGenerator := Rc4Cipher random. |
136 |
^ SharedRandomGenerator. |
|
2432 | 137 |
]. |
138 |
||
3193 | 139 |
SharedRandomGenerator := Random sharedGenerator. |
140 |
^ SharedRandomGenerator. |
|
1606 | 141 |
! ! |
142 |
||
2432 | 143 |
!RandomGenerator class methodsFor:'adding entropy'! |
144 |
||
145 |
addEntropy:entropyBytes |
|
3193 | 146 |
SharedRandomGenerator notNil ifTrue:[ |
147 |
SharedRandomGenerator addEntropy:entropyBytes |
|
2432 | 148 |
]. |
149 |
! ! |
|
150 |
||
1606 | 151 |
!RandomGenerator class methodsFor:'change & update'! |
152 |
||
153 |
update:something with:aParameter from:changedObject |
|
154 |
"handle image restarts and flush any device resource handles" |
|
155 |
||
3193 | 156 |
SharedRandomGenerator := nil. |
1606 | 157 |
RandFile notNil ifTrue:[ |
158 |
RandFile := nil. |
|
159 |
self openRandFile. |
|
160 |
]. |
|
161 |
! ! |
|
162 |
||
163 |
!RandomGenerator class methodsFor:'queries'! |
|
164 |
||
165 |
randPath |
|
166 |
"path to a file/device that is a source or random numbers" |
|
167 |
||
168 |
OperatingSystem isUNIXlike ifTrue:[^ '/dev/urandom']. |
|
169 |
^ nil. |
|
170 |
! ! |
|
171 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
172 |
!RandomGenerator methodsFor:'basic reading'! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
173 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
174 |
nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
175 |
"get the next random byte" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
176 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
177 |
RandFile == true ifTrue:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
178 |
^ OperatingSystem randomBytesInto:1. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
179 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
180 |
|
3632 | 181 |
RandFile isOpen ifFalse:[ |
182 |
self class openRandFile. |
|
183 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
184 |
^ RandFile nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
185 |
|
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 |
RandomGenerator new nextByte |
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 |
|
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 |
Distribution should be equal: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
192 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
193 |
|r bag| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
194 |
r := RandomGenerator new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
195 |
bag := Bag new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
196 |
1000000 timesRepeat:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
197 |
bag add:(r nextByte). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
198 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
199 |
bag. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
200 |
Transcript showCR:bag contents |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
201 |
" |
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 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
204 |
! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
205 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
206 |
nextBytes:cnt |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
207 |
"get the next cnt random bytes" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
208 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
209 |
RandFile == true ifTrue:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
210 |
^ OperatingSystem randomBytesInto:(ByteArray new:cnt). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
211 |
]. |
3632 | 212 |
RandFile isOpen ifFalse:[ |
213 |
self class openRandFile. |
|
214 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
215 |
^ RandFile nextBytes:cnt. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
216 |
|
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 |
RandomGenerator new nextBytes:4 |
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 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
221 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
222 |
"Modified: / 11.11.1999 / 09:52:26 / stefan" |
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 |
|
1606 | 225 |
!RandomGenerator methodsFor:'reading'! |
226 |
||
227 |
next |
|
228 |
"return the next random number in the range 0..1" |
|
229 |
||
230 |
^ self nextInteger / 16r3fffffff asFloat |
|
231 |
||
232 |
" |
|
233 |
|r| |
|
234 |
r := RandomGenerator new. |
|
235 |
Transcript showCR:r next. |
|
236 |
Transcript showCR:r next. |
|
237 |
Transcript showCR:r next. |
|
238 |
Transcript showCR:r next. |
|
239 |
" |
|
240 |
||
241 |
"Modified: / 11.11.1999 / 10:31:35 / stefan" |
|
242 |
! |
|
243 |
||
244 |
nextBoolean |
|
245 |
"return true or false by random" |
|
246 |
||
247 |
^ self nextByte <= 127 |
|
248 |
||
249 |
" |
|
250 |
|r| |
|
251 |
r := RandomGenerator new. |
|
252 |
Transcript showCR:r nextBoolean. |
|
253 |
Transcript showCR:r nextBoolean. |
|
254 |
Transcript showCR:r nextBoolean. |
|
255 |
Transcript showCR:r nextBoolean. |
|
256 |
" |
|
257 |
||
258 |
" |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
259 |
Distribution should approach 50/50: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
260 |
|
1606 | 261 |
|r bag| |
262 |
r := RandomGenerator new. |
|
263 |
bag := Bag new. |
|
264 |
1000000 timesRepeat:[ |
|
265 |
bag add:(r nextBoolean). |
|
266 |
]. |
|
267 |
Transcript showCR:bag contents |
|
268 |
" |
|
269 |
||
270 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
271 |
"Modified: / 12.11.1999 / 17:22:01 / stefan" |
|
272 |
! |
|
273 |
||
1640 | 274 |
nextCharacters:cnt |
275 |
"get the next cnt printable characters. |
|
276 |
We answer characters in the ascii range (codepoints 32 - 127)" |
|
277 |
||
278 |
|bytes string| |
|
279 |
||
280 |
bytes := self nextBytes:cnt. |
|
281 |
string := String new:cnt. |
|
282 |
||
283 |
bytes keysAndValuesDo:[:eachIndex :eachByte| |
|
284 |
string at:eachIndex put:(Character value:(eachByte \\ 95 + 32)). |
|
285 |
]. |
|
286 |
||
287 |
^ string |
|
288 |
||
289 |
||
290 |
" |
|
291 |
RandomGenerator new nextCharacters:8 |
|
292 |
" |
|
293 |
! |
|
294 |
||
3632 | 295 |
nextInteger |
296 |
"return the next integral random number, |
|
297 |
in the range 0 .. 16r3FFFFFFF." |
|
298 |
||
299 |
|res| |
|
300 |
||
301 |
RandFile == true ifTrue:[ |
|
302 |
^ OperatingSystem randomBytesInto:4. |
|
303 |
]. |
|
304 |
||
305 |
res := self nextBytes:4. |
|
306 |
^ ((((((res at:1) bitAnd:16r3F) * 256) + (res at:2)) * 256) + (res at:3)) * 256 + (res at:4) |
|
307 |
||
308 |
||
309 |
||
310 |
" |
|
311 |
|r| |
|
312 |
r := RandomGenerator new. |
|
313 |
Transcript showCR:r nextInteger. |
|
314 |
Transcript showCR:r nextInteger. |
|
315 |
Transcript showCR:r nextInteger. |
|
316 |
Transcript showCR:r nextInteger. |
|
317 |
" |
|
318 |
||
319 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
320 |
! |
|
321 |
||
1606 | 322 |
nextIntegerBetween:start and:stop |
323 |
"return an integral random number between start and stop" |
|
324 |
||
3025 | 325 |
|rnd range bytesNeeded| |
1606 | 326 |
|
3025 | 327 |
range := stop - start + 1. |
3133 | 328 |
bytesNeeded := (range highBit + 15) // 8. |
3025 | 329 |
"Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges" |
330 |
rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed. |
|
331 |
rnd := rnd \\ range. |
|
332 |
^ rnd + start |
|
1606 | 333 |
|
334 |
" |
|
335 |
|r| |
|
3025 | 336 |
r := self new. |
1606 | 337 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
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 |
" |
|
342 |
||
343 |
" |
|
344 |
|r bag| |
|
3025 | 345 |
r := self new. |
1606 | 346 |
bag := Bag new. |
347 |
1000000 timesRepeat:[ |
|
348 |
bag add:(r nextIntegerBetween:-1 and:1). |
|
349 |
]. |
|
3133 | 350 |
Transcript showCR:bag sortedCounts. |
3025 | 351 |
" |
352 |
||
353 |
" |
|
354 |
|r bag| |
|
355 |
r := self new. |
|
356 |
bag := Bag new. |
|
357 |
1000000 timesRepeat:[ |
|
358 |
bag add:(r nextIntegerBetween:1 and:3). |
|
359 |
]. |
|
3133 | 360 |
Transcript showCR:bag sortedCounts. |
3025 | 361 |
TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)). |
362 |
" |
|
363 |
||
364 |
" |
|
365 |
|r bag| |
|
366 |
r := self new. |
|
367 |
bag := Bag new. |
|
368 |
1000000 timesRepeat:[ |
|
369 |
bag add:(r nextIntegerBetween:1 and:10). |
|
370 |
]. |
|
3133 | 371 |
Transcript showCR:bag sortedCounts. |
3025 | 372 |
TestCase assert:(bag standardDeviation closeTo:(((10 squared - 1)/12) sqrt)). |
1606 | 373 |
" |
374 |
||
375 |
"Created: / 11.11.1999 / 10:28:36 / stefan" |
|
376 |
! ! |
|
377 |
||
378 |
!RandomGenerator methodsFor:'writing'! |
|
379 |
||
380 |
nextPut:something |
|
381 |
"change the random pool by feeding in something. |
|
382 |
Something should be some unpredictable, random event. |
|
383 |
Ignored here" |
|
384 |
||
385 |
||
386 |
! |
|
387 |
||
388 |
nextPutAll:something |
|
389 |
"change the random pool by feeding in something. |
|
390 |
Something should be some unpredictable, random event. |
|
391 |
Ignored here" |
|
392 |
||
393 |
! ! |
|
394 |
||
395 |
!RandomGenerator class methodsFor:'documentation'! |
|
396 |
||
397 |
version |
|
3632 | 398 |
^ '$Header$' |
2432 | 399 |
! |
400 |
||
401 |
version_CVS |
|
3632 | 402 |
^ '$Header$' |
1606 | 403 |
! ! |
404 |
||
3025 | 405 |
|
1606 | 406 |
RandomGenerator initialize! |