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