author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 5213 | 7d2ae06d2fae |
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:'' |
4597 | 20 |
category:'Magnitude-Numbers-Random' |
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 |
||
4482 | 45 |
Warning: |
46 |
this generator should not be used for cryptographic work |
|
3777 | 47 |
UNLESS: |
3351
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
48 |
1) you are running on linux, solaris or osx, |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
49 |
2) you have a working /dev/urandom. |
a9ae920e4a1f
comment/format in: #documentation
Claus Gittinger <cg@exept.de>
parents:
3339
diff
changeset
|
50 |
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
|
51 |
|
4482 | 52 |
It is a facade one one of the other random generators, |
53 |
and will choose (in decreasing priority) the first available generator from the following: |
|
54 |
||
55 |
- OS random system call |
|
56 |
- /dev/urandom |
|
57 |
- rc4-based random (if present) |
|
58 |
- regular (untrusted) random as fallback |
|
59 |
||
1606 | 60 |
[author:] |
61 |
Stefan Vogel |
|
62 |
||
63 |
[see also:] |
|
3391 | 64 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf |
1606 | 65 |
Random HashRandom Rc4Stream |
66 |
||
67 |
[instance variables:] |
|
68 |
||
69 |
[class variables:] |
|
3777 | 70 |
HasOSRandom true if the operatingSystem supports random numbers |
71 |
RandFile (if HasOSRandom is false and non-nil) the FileStream (/dev/random), |
|
72 |
we get random numbers from |
|
1606 | 73 |
" |
74 |
! ! |
|
75 |
||
76 |
!RandomGenerator class methodsFor:'initialization'! |
|
77 |
||
78 |
initialize |
|
79 |
"want to be informed when returning from snapshot" |
|
80 |
||
81 |
ObjectMemory addDependent:self. |
|
82 |
||
83 |
||
84 |
! |
|
85 |
||
86 |
openRandFile |
|
87 |
"try to open a random device" |
|
88 |
||
89 |
|randDevName| |
|
90 |
||
3777 | 91 |
RandFile notNil ifTrue:[ |
1606 | 92 |
RandFile close. |
3777 | 93 |
RandFile := nil. |
1606 | 94 |
]. |
95 |
||
96 |
randDevName := self randPath. |
|
97 |
randDevName notNil ifTrue:[ |
|
98 |
randDevName := randDevName asFilename. |
|
99 |
randDevName isReadable ifTrue:[ |
|
3777 | 100 |
RandFile := randDevName readStream. |
1606 | 101 |
]. |
102 |
]. |
|
103 |
! ! |
|
104 |
||
105 |
!RandomGenerator class methodsFor:'instance creation'! |
|
106 |
||
107 |
new |
|
108 |
"return a new random number generator. |
|
4250 | 109 |
Try to get system random numbers from OS (e.g. in LINUX) |
110 |
or device urandom (e.g. in Linux, OSX). |
|
4025 | 111 |
If no system random numbers are available, fall back to |
4250 | 112 |
a cryptographic secure PRNG (like RC4Random, part of the extra libcrypt package). |
1606 | 113 |
As last resort fallback to the cryptographic insecure linear builtin PRNG" |
114 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
115 |
|result| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
116 |
|
3193 | 117 |
SharedRandomGenerator notNil ifTrue:[ |
4602 | 118 |
"each time, we do a new, add some entropy to the SharedRandomGenerator" |
3193 | 119 |
SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime. |
120 |
^ SharedRandomGenerator. |
|
2432 | 121 |
]. |
122 |
||
3854 | 123 |
HasOSRandom isNil ifTrue:[ |
3777 | 124 |
"/ see if there is an OS or CPU-random generator |
125 |
[ |
|
126 |
"fetch a random byte - and check if the OS generator works" |
|
127 |
result := OperatingSystem randomBytesInto:1. |
|
128 |
] on:PrimitiveFailure do:[:ex| ]. |
|
129 |
HasOSRandom := result notNil. |
|
130 |
]. |
|
4025 | 131 |
|
3777 | 132 |
HasOSRandom ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
133 |
"OperatingSystem knows how to get random bytes" |
3193 | 134 |
SharedRandomGenerator := self basicNew. |
3203 | 135 |
^ SharedRandomGenerator. |
3777 | 136 |
]. |
4025 | 137 |
|
3777 | 138 |
RandFile isNil ifTrue:[ |
139 |
self openRandFile. |
|
140 |
]. |
|
141 |
RandFile notNil ifTrue:[ |
|
142 |
SharedRandomGenerator := self basicNew. |
|
143 |
^ SharedRandomGenerator. |
|
1606 | 144 |
]. |
145 |
||
4025 | 146 |
"/ fallback |
2432 | 147 |
Rc4Cipher notNil ifTrue:[ |
3193 | 148 |
SharedRandomGenerator := Rc4Cipher random. |
149 |
^ SharedRandomGenerator. |
|
2432 | 150 |
]. |
151 |
||
4025 | 152 |
"/ last fallback - not very good |
3193 | 153 |
SharedRandomGenerator := Random sharedGenerator. |
154 |
^ SharedRandomGenerator. |
|
1606 | 155 |
! ! |
156 |
||
2432 | 157 |
!RandomGenerator class methodsFor:'adding entropy'! |
158 |
||
159 |
addEntropy:entropyBytes |
|
3193 | 160 |
SharedRandomGenerator notNil ifTrue:[ |
161 |
SharedRandomGenerator addEntropy:entropyBytes |
|
2432 | 162 |
]. |
163 |
! ! |
|
164 |
||
1606 | 165 |
!RandomGenerator class methodsFor:'change & update'! |
166 |
||
167 |
update:something with:aParameter from:changedObject |
|
168 |
"handle image restarts and flush any device resource handles" |
|
169 |
||
3777 | 170 |
(changedObject == ObjectMemory) ifTrue:[ |
171 |
(something == #returnFromSnapshot) ifTrue:[ |
|
172 |
SharedRandomGenerator notNil ifTrue:[ |
|
173 |
SharedRandomGenerator := nil. |
|
174 |
HasOSRandom := RandFile := nil. |
|
4602 | 175 |
self new. "/ opens a new SharedRandomGenerator |
3777 | 176 |
] |
177 |
] |
|
1606 | 178 |
]. |
179 |
! ! |
|
180 |
||
181 |
!RandomGenerator class methodsFor:'queries'! |
|
182 |
||
183 |
randPath |
|
184 |
"path to a file/device that is a source or random numbers" |
|
185 |
||
4041 | 186 |
|pathsOrNil| |
3777 | 187 |
|
4041 | 188 |
pathsOrNil := self randomDevicePathes. |
189 |
pathsOrNil notNil ifTrue:[ |
|
190 |
pathsOrNil do:[:triedRandom | |
|
3777 | 191 |
triedRandom asFilename exists ifTrue:[ |
192 |
^ triedRandom |
|
193 |
] |
|
4041 | 194 |
]. |
3777 | 195 |
]. |
1606 | 196 |
^ nil. |
3777 | 197 |
! |
198 |
||
199 |
randomDevicePathes |
|
4250 | 200 |
"paths to look for OS sources of random numbers. |
201 |
Can be configured at early startup to use something special |
|
202 |
(i.e. a special hardware random device) |
|
203 |
If never set, the defaults /dev/random, /dev/urandom are used" |
|
3777 | 204 |
|
205 |
RandomDevicePathes notNil ifTrue:[^ RandomDevicePathes]. |
|
206 |
OperatingSystem isUNIXlike ifTrue:[ |
|
207 |
^ #( '/dev/urandom' '/dev/random' ) |
|
208 |
]. |
|
209 |
^ nil. |
|
210 |
! |
|
211 |
||
4250 | 212 |
randomDevicePathes:aCollectionOfPathesOrNil |
4041 | 213 |
"configurable paths to look for OS sources of random numbers. |
4250 | 214 |
(can be set during early startup in an rc-file). |
215 |
If never set, the defaults /dev/random, /dev/urandom are used" |
|
3777 | 216 |
|
4250 | 217 |
RandomDevicePathes := aCollectionOfPathesOrNil. |
1606 | 218 |
! ! |
219 |
||
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
220 |
!RandomGenerator methodsFor:'basic reading'! |
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 |
nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
223 |
"get the next random byte" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
224 |
|
3777 | 225 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
226 |
^ OperatingSystem randomBytesInto:1. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
227 |
]. |
3632 | 228 |
RandFile isOpen ifFalse:[ |
229 |
self class openRandFile. |
|
230 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
231 |
^ RandFile nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
232 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
233 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
234 |
RandomGenerator new nextByte |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
235 |
" |
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 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
238 |
Distribution should be equal: |
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 |
|r bag| |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
241 |
r := RandomGenerator new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
242 |
bag := Bag new. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
243 |
1000000 timesRepeat:[ |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
244 |
bag add:(r nextByte). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
245 |
]. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
246 |
bag. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
247 |
Transcript showCR:bag contents |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
248 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
249 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
250 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
251 |
! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
252 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
253 |
nextBytes:cnt |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
254 |
"get the next cnt random bytes" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
255 |
|
3777 | 256 |
HasOSRandom == true ifTrue:[ |
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
257 |
^ OperatingSystem randomBytesInto:(ByteArray new:cnt). |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
258 |
]. |
3632 | 259 |
RandFile isOpen ifFalse:[ |
260 |
self class openRandFile. |
|
261 |
]. |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
262 |
^ RandFile nextBytes:cnt. |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
263 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
264 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
265 |
RandomGenerator new nextBytes:4 |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
266 |
" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
267 |
|
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
268 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
269 |
"Modified: / 11.11.1999 / 09:52:26 / stefan" |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
270 |
! ! |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
271 |
|
1606 | 272 |
!RandomGenerator methodsFor:'reading'! |
273 |
||
4602 | 274 |
maxInteger |
275 |
"return the max integral random number returned by nextInteger" |
|
276 |
||
277 |
SmallInteger maxBytes == 8 ifTrue:[ |
|
278 |
^ 16r3FFFFFFFFFFFFFFF |
|
279 |
] ifFalse:[ |
|
280 |
^ 16r3FFFFFFF |
|
281 |
]. |
|
282 |
||
283 |
" |
|
284 |
RandomGenerator new maxInteger. |
|
285 |
" |
|
286 |
||
287 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
288 |
! |
|
289 |
||
1606 | 290 |
next |
291 |
"return the next random number in the range 0..1" |
|
292 |
||
3751 | 293 |
^ self nextInteger / Float maxSmallInteger |
1606 | 294 |
|
295 |
" |
|
296 |
|r| |
|
297 |
r := RandomGenerator new. |
|
298 |
Transcript showCR:r next. |
|
299 |
Transcript showCR:r next. |
|
300 |
Transcript showCR:r next. |
|
301 |
Transcript showCR:r next. |
|
302 |
" |
|
303 |
||
304 |
"Modified: / 11.11.1999 / 10:31:35 / stefan" |
|
305 |
! |
|
306 |
||
307 |
nextBoolean |
|
308 |
"return true or false by random" |
|
309 |
||
310 |
^ self nextByte <= 127 |
|
311 |
||
312 |
" |
|
313 |
|r| |
|
314 |
r := RandomGenerator new. |
|
315 |
Transcript showCR:r nextBoolean. |
|
316 |
Transcript showCR:r nextBoolean. |
|
317 |
Transcript showCR:r nextBoolean. |
|
318 |
Transcript showCR:r nextBoolean. |
|
319 |
" |
|
320 |
||
321 |
" |
|
2439
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
322 |
Distribution should approach 50/50: |
8ef4ad63fb9b
comment/format in: #nextBoolean
Stefan Vogel <sv@exept.de>
parents:
2438
diff
changeset
|
323 |
|
1606 | 324 |
|r bag| |
325 |
r := RandomGenerator new. |
|
326 |
bag := Bag new. |
|
327 |
1000000 timesRepeat:[ |
|
328 |
bag add:(r nextBoolean). |
|
329 |
]. |
|
330 |
Transcript showCR:bag contents |
|
331 |
" |
|
332 |
||
333 |
"Created: / 11.11.1999 / 09:25:39 / stefan" |
|
334 |
"Modified: / 12.11.1999 / 17:22:01 / stefan" |
|
335 |
! |
|
336 |
||
3751 | 337 |
nextCharacters:count |
338 |
"get the next count printable characters. |
|
339 |
We answer printable characters in the ascii range (codepoints 32 - 126)" |
|
1640 | 340 |
|
3751 | 341 |
|bytes string cnt "{ Class:SmallInteger }"| |
1640 | 342 |
|
3751 | 343 |
cnt := count. |
1640 | 344 |
bytes := self nextBytes:cnt. |
345 |
string := String new:cnt. |
|
346 |
||
3751 | 347 |
1 to:cnt do:[:eachIndex| |
348 |
string at:eachIndex put:(Character value:((bytes at:eachIndex) \\ 94 + 32)). |
|
1640 | 349 |
]. |
350 |
||
351 |
^ string |
|
352 |
||
353 |
" |
|
354 |
RandomGenerator new nextCharacters:8 |
|
355 |
" |
|
356 |
! |
|
357 |
||
3632 | 358 |
nextInteger |
359 |
"return the next integral random number, |
|
3751 | 360 |
in the range 0 .. 16r3FFFFFFF (32-bit) |
361 |
or 0 .. 16r3FFFFFFFFFFFFFFF (64-bit)." |
|
3632 | 362 |
|
3751 | 363 |
|res intSize| |
364 |
||
365 |
intSize := SmallInteger maxBytes. |
|
3632 | 366 |
|
3777 | 367 |
HasOSRandom == true ifTrue:[ |
3751 | 368 |
^ OperatingSystem randomBytesInto:intSize. |
3632 | 369 |
]. |
370 |
||
3751 | 371 |
res := self nextBytes:intSize. |
372 |
res at:intSize put:((res at:intSize) bitAnd:16r3F). |
|
5213 | 373 |
intSize == 8 ifTrue:[ |
374 |
^ res unsignedInt64At:1 MSB:false. |
|
375 |
]. |
|
376 |
^ res unsignedInt32At:1 MSB:false. |
|
3632 | 377 |
|
378 |
||
379 |
" |
|
380 |
|r| |
|
381 |
r := RandomGenerator new. |
|
382 |
Transcript showCR:r nextInteger. |
|
383 |
Transcript showCR:r nextInteger. |
|
384 |
Transcript showCR:r nextInteger. |
|
385 |
Transcript showCR:r nextInteger. |
|
386 |
" |
|
387 |
||
388 |
"Modified: / 11.11.1999 / 10:08:10 / stefan" |
|
389 |
! |
|
390 |
||
3751 | 391 |
nextLettersOrDigits:count |
392 |
"get the next count printable letters or digits [0-9A-Za-z]." |
|
393 |
||
394 |
|bytes string cnt "{ Class:SmallInteger }"| |
|
395 |
||
396 |
cnt := count. |
|
397 |
bytes := self nextBytes:cnt. |
|
398 |
string := String new:cnt. |
|
399 |
||
400 |
1 to:cnt do:[:eachIndex| |
|
401 |
string |
|
402 |
at:eachIndex |
|
403 |
put:('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' at:((bytes at:eachIndex) \\ 62 + 1)). |
|
404 |
]. |
|
405 |
||
406 |
^ string |
|
407 |
||
408 |
" |
|
409 |
RandomGenerator new nextLettersOrDigits:40 |
|
410 |
" |
|
1606 | 411 |
! ! |
412 |
||
413 |
!RandomGenerator methodsFor:'writing'! |
|
414 |
||
415 |
nextPut:something |
|
416 |
"change the random pool by feeding in something. |
|
417 |
Something should be some unpredictable, random event. |
|
418 |
Ignored here" |
|
419 |
||
5176 | 420 |
^ something |
1606 | 421 |
! |
422 |
||
423 |
nextPutAll:something |
|
424 |
"change the random pool by feeding in something. |
|
425 |
Something should be some unpredictable, random event. |
|
426 |
Ignored here" |
|
427 |
||
428 |
! ! |
|
429 |
||
430 |
!RandomGenerator class methodsFor:'documentation'! |
|
431 |
||
432 |
version |
|
3632 | 433 |
^ '$Header$' |
2432 | 434 |
! |
435 |
||
436 |
version_CVS |
|
3632 | 437 |
^ '$Header$' |
1606 | 438 |
! ! |
439 |
||
3025 | 440 |
|
1606 | 441 |
RandomGenerator initialize! |