author | Stefan Vogel <sv@exept.de> |
Tue, 30 Mar 2010 14:32:10 +0200 | |
changeset 2438 | 3601d0065aff |
parent 2432 | 77c3b2c22e9c |
child 2439 | 8ef4ad63fb9b |
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 |
||
14 |
Object subclass:#RandomGenerator |
|
15 |
instanceVariableNames:'' |
|
2432 | 16 |
classVariableNames:'RandFile SharedGenerator' |
1606 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-Numbers' |
|
19 |
! |
|
20 |
||
21 |
!RandomGenerator class methodsFor:'documentation'! |
|
22 |
||
1925 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
1606 | 37 |
documentation |
38 |
" |
|
39 |
This is a Random number generator, which uses either a OS random number generator, |
|
40 |
or a ST/X internal random number generator. |
|
41 |
||
42 |
[author:] |
|
43 |
Stefan Vogel |
|
44 |
||
45 |
[see also:] |
|
46 |
Random HashRandom Rc4Stream |
|
47 |
||
48 |
[instance variables:] |
|
49 |
||
50 |
[class variables:] |
|
51 |
RandFile the FileStream we get random numbers from |
|
52 |
" |
|
53 |
! ! |
|
54 |
||
55 |
!RandomGenerator class methodsFor:'initialization'! |
|
56 |
||
57 |
initialize |
|
58 |
"want to be informed when returning from snapshot" |
|
59 |
||
60 |
ObjectMemory addDependent:self. |
|
61 |
||
62 |
||
63 |
! |
|
64 |
||
65 |
openRandFile |
|
66 |
"try to open a random device" |
|
67 |
||
68 |
|randDevName| |
|
69 |
||
70 |
RandFile isStream ifTrue:[ |
|
71 |
RandFile close. |
|
72 |
]. |
|
73 |
||
74 |
RandFile := false. "prevent retry" |
|
75 |
||
76 |
randDevName := self randPath. |
|
77 |
randDevName notNil ifTrue:[ |
|
78 |
randDevName := randDevName asFilename. |
|
79 |
randDevName isReadable ifTrue:[ |
|
80 |
RandFile := randDevName readStream |
|
81 |
]. |
|
82 |
]. |
|
83 |
! ! |
|
84 |
||
85 |
!RandomGenerator class methodsFor:'instance creation'! |
|
86 |
||
87 |
new |
|
88 |
"return a new random number generator. |
|
89 |
Try to get system random numbers from device (e.g. in LINUX). |
|
90 |
If no system random nubers are available, fall back to |
|
91 |
a cryptographic secure PRNG (part of the extra libcrypt package). |
|
92 |
As last resort fallback to the cryptographic insecure linear builtin PRNG" |
|
93 |
||
2432 | 94 |
SharedGenerator notNil ifTrue:[ |
95 |
"each time, we do an new, add some entropy to the SharedGenerator" |
|
2438
3601d0065aff
changed: #new - use microSecondTime to add entropy
Stefan Vogel <sv@exept.de>
parents:
2432
diff
changeset
|
96 |
SharedGenerator addEntropy:OperatingSystem getMicrosecondTime. |
2432 | 97 |
^ SharedGenerator. |
98 |
]. |
|
99 |
||
1606 | 100 |
RandFile isNil ifTrue:[ |
101 |
self openRandFile. |
|
102 |
]. |
|
103 |
RandFile isStream ifTrue:[ |
|
2432 | 104 |
SharedGenerator := self basicNew. |
105 |
^ SharedGenerator. |
|
1606 | 106 |
]. |
107 |
||
2432 | 108 |
Rc4Cipher notNil ifTrue:[ |
109 |
SharedGenerator := Rc4Cipher random. |
|
110 |
^ SharedGenerator. |
|
111 |
]. |
|
112 |
||
113 |
SharedGenerator := Random sharedGenerator. |
|
114 |
^ SharedGenerator. |
|
1606 | 115 |
! |
116 |
||
117 |
random |
|
118 |
"alias for new - protocol compatibility with StreamCiphers" |
|
119 |
||
120 |
^ self new |
|
121 |
! ! |
|
122 |
||
2432 | 123 |
!RandomGenerator class methodsFor:'adding entropy'! |
124 |
||
125 |
addEntropy:entropyBytes |
|
126 |
SharedGenerator notNil ifTrue:[ |
|
127 |
SharedGenerator addEntropy:entropyBytes |
|
128 |
]. |
|
129 |
! ! |
|
130 |
||
1606 | 131 |
!RandomGenerator class methodsFor:'change & update'! |
132 |
||
133 |
update:something with:aParameter from:changedObject |
|
134 |
"handle image restarts and flush any device resource handles" |
|
135 |
||
2432 | 136 |
SharedGenerator := nil. |
1606 | 137 |
RandFile notNil ifTrue:[ |
138 |
RandFile := nil. |
|
139 |
self openRandFile. |
|
140 |
]. |
|
141 |
! ! |
|
142 |
||
143 |
!RandomGenerator class methodsFor:'queries'! |
|
144 |
||
145 |
randPath |
|
146 |
"path to a file/device that is a source or random numbers" |
|
147 |
||
148 |
OperatingSystem isUNIXlike ifTrue:[^ '/dev/urandom']. |
|
149 |
^ nil. |
|
150 |
! ! |
|
151 |
||
2432 | 152 |
!RandomGenerator methodsFor:'adding entropy'! |
153 |
||
154 |
addEntropy:entropyBytes |
|
155 |
"add some entropy - ignored here, since I am file based" |
|
156 |
||
157 |
^ self |
|
158 |
! ! |
|
159 |
||
1606 | 160 |
!RandomGenerator methodsFor:'reading'! |
161 |
||
162 |
next |
|
163 |
"return the next random number in the range 0..1" |
|
164 |
||
165 |
^ self nextInteger / 16r3fffffff asFloat |
|
166 |
||
167 |
" |
|
168 |
|r| |
|
169 |
r := RandomGenerator new. |
|
170 |
Transcript showCR:r next. |
|
171 |
Transcript showCR:r next. |
|
172 |
Transcript showCR:r next. |
|
173 |
Transcript showCR:r next. |
|
174 |
" |
|
175 |
||
176 |
"Modified: / 11.11.1999 / 10:31:35 / stefan" |
|
177 |
! |
|
178 |
||
179 |
nextBetween:start and:stop |
|
180 |
"return a random number between start and stop." |
|
181 |
||
182 |
|rnd| |
|
183 |
||
184 |
rnd := self next. |
|
185 |
rnd := rnd * (stop - start) asFloat. |
|
186 |
rnd := rnd + start asFloat. |
|
187 |
^ rnd |
|
188 |
||
189 |
" |
|
190 |
|r| |
|
191 |
r := RandomGenerator new. |
|
192 |
Transcript showCR:(r nextBetween:1 and:10). |
|
193 |
Transcript showCR:(r nextBetween:1 and:10). |
|
194 |
Transcript showCR:(r nextBetween:1 and:10). |
|
195 |
Transcript showCR:(r nextBetween:1 and:10). |
|
196 |
" |
|
197 |
||
198 |
"Created: / 11.11.1999 / 10:27:56 / stefan" |
|
199 |
! |
|
200 |
||
201 |
nextBoolean |
|
202 |
"return true or false by random" |
|
203 |
||
204 |
^ self nextByte <= 127 |
|
205 |
||
206 |
" |
|
207 |
|r| |
|
208 |
r := RandomGenerator new. |
|
209 |
Transcript showCR:r nextBoolean. |
|
210 |
Transcript showCR:r nextBoolean. |
|
211 |
Transcript showCR:r nextBoolean. |
|
212 |
Transcript showCR:r nextBoolean. |
|
213 |
" |
|
214 |
||
215 |
" |
|
216 |
|r bag| |
|
217 |
r := RandomGenerator new. |
|
218 |
bag := Bag new. |
|
219 |
1000000 timesRepeat:[ |
|
220 |
bag add:(r nextBoolean). |
|
221 |
]. |
|
222 |
Transcript showCR:bag contents |
|
223 |
" |
|
224 |
||
225 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
226 |
"Modified: / 12.11.1999 / 17:22:01 / stefan" |
|
227 |
! |
|
228 |
||
229 |
nextByte |
|
230 |
"get the next random byte" |
|
231 |
||
232 |
^ RandFile nextByte |
|
233 |
||
234 |
" |
|
235 |
RandomGenerator new nextByte |
|
236 |
" |
|
237 |
||
238 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
239 |
! |
|
240 |
||
241 |
nextBytes:cnt |
|
242 |
"get the next cnt random bytes" |
|
243 |
||
244 |
^ RandFile nextBytes:cnt. |
|
245 |
||
246 |
" |
|
247 |
RandomGenerator new nextBytes:4 |
|
248 |
" |
|
249 |
||
250 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
251 |
"Modified: / 11.11.1999 / 09:52:26 / stefan" |
|
252 |
! |
|
253 |
||
1640 | 254 |
nextCharacters:cnt |
255 |
"get the next cnt printable characters. |
|
256 |
We answer characters in the ascii range (codepoints 32 - 127)" |
|
257 |
||
258 |
|bytes string| |
|
259 |
||
260 |
bytes := self nextBytes:cnt. |
|
261 |
string := String new:cnt. |
|
262 |
||
263 |
bytes keysAndValuesDo:[:eachIndex :eachByte| |
|
264 |
string at:eachIndex put:(Character value:(eachByte \\ 95 + 32)). |
|
265 |
]. |
|
266 |
||
267 |
^ string |
|
268 |
||
269 |
||
270 |
" |
|
271 |
RandomGenerator new nextCharacters:8 |
|
272 |
" |
|
273 |
! |
|
274 |
||
1606 | 275 |
nextInteger |
276 |
"return the next integral random number, |
|
277 |
in the range 0 .. 16r3FFFFFFF." |
|
278 |
||
279 |
|res| |
|
280 |
||
281 |
res := self nextBytes:4. |
|
282 |
^ ((((((res at:1) bitAnd:16r3F) * 256) + (res at:2)) * 256) + (res at:3)) * 256 + (res at:4) |
|
283 |
||
284 |
||
285 |
||
286 |
" |
|
287 |
|r| |
|
288 |
r := RandomGenerator new. |
|
289 |
Transcript showCR:r nextInteger. |
|
290 |
Transcript showCR:r nextInteger. |
|
291 |
Transcript showCR:r nextInteger. |
|
292 |
Transcript showCR:r nextInteger. |
|
293 |
" |
|
294 |
||
295 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
296 |
! |
|
297 |
||
298 |
nextIntegerBetween:start and:stop |
|
299 |
"return an integral random number between start and stop" |
|
300 |
||
301 |
|rnd| |
|
302 |
||
303 |
rnd := self next. |
|
304 |
rnd := rnd * ((stop - start) asFloat + 1.0). |
|
305 |
^ rnd truncated + start |
|
306 |
||
307 |
" |
|
308 |
|r| |
|
309 |
r := RandomGenerator new. |
|
310 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
311 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
312 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
313 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
314 |
" |
|
315 |
||
316 |
" |
|
317 |
|r bag| |
|
318 |
r := RandomGenerator new. |
|
319 |
bag := Bag new. |
|
320 |
1000000 timesRepeat:[ |
|
321 |
bag add:(r nextIntegerBetween:-1 and:1). |
|
322 |
]. |
|
323 |
Transcript showCR:bag contents |
|
324 |
" |
|
325 |
||
326 |
"Created: / 11.11.1999 / 10:28:36 / stefan" |
|
327 |
! |
|
328 |
||
329 |
nextMatchFor: aNumber |
|
330 |
"generate the next random, return true iff it has the same |
|
331 |
value as aNumber. Redefined to avoid endless reading." |
|
332 |
||
1840 | 333 |
^ self next = aNumber |
1606 | 334 |
|
335 |
"Created: / 11.11.1999 / 10:29:32 / stefan" |
|
336 |
! ! |
|
337 |
||
338 |
!RandomGenerator methodsFor:'writing'! |
|
339 |
||
340 |
nextPut:something |
|
341 |
"change the random pool by feeding in something. |
|
342 |
Something should be some unpredictable, random event. |
|
343 |
Ignored here" |
|
344 |
||
345 |
||
346 |
! |
|
347 |
||
348 |
nextPutAll:something |
|
349 |
"change the random pool by feeding in something. |
|
350 |
Something should be some unpredictable, random event. |
|
351 |
Ignored here" |
|
352 |
||
353 |
! ! |
|
354 |
||
355 |
!RandomGenerator class methodsFor:'documentation'! |
|
356 |
||
357 |
version |
|
2438
3601d0065aff
changed: #new - use microSecondTime to add entropy
Stefan Vogel <sv@exept.de>
parents:
2432
diff
changeset
|
358 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomGenerator.st,v 1.6 2010-03-30 12:32:10 stefan Exp $' |
2432 | 359 |
! |
360 |
||
361 |
version_CVS |
|
2438
3601d0065aff
changed: #new - use microSecondTime to add entropy
Stefan Vogel <sv@exept.de>
parents:
2432
diff
changeset
|
362 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomGenerator.st,v 1.6 2010-03-30 12:32:10 stefan Exp $' |
1606 | 363 |
! ! |
364 |
||
365 |
RandomGenerator initialize! |