3356
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#RandomKISS
|
|
4 |
instanceVariableNames:'x y z c'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Magnitude-Numbers'
|
|
8 |
!
|
|
9 |
|
|
10 |
!RandomKISS class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Warning: this generator should not be used for cryptographic work.
|
|
15 |
|
|
16 |
NO WARRANTY
|
|
17 |
|
|
18 |
KISS combines a linear congruence, XorShift and Lag with carry geenrator.
|
|
19 |
The idea is from George Marsaglia.
|
|
20 |
|
|
21 |
RandomKISS new nextInteger
|
|
22 |
(RandomKISS new:5489) nextInteger
|
|
23 |
|
|
24 |
If heavily used, it may be useful to embed the original C code as inline C code for a big speedup)
|
|
25 |
|
|
26 |
Please read:
|
|
27 |
Wikipedia article on KISS http://de.wikipedia.org/wiki/KISS_(Zufallszahlengenerator)
|
|
28 |
http://de.wikipedia.org/wiki/Liste_von_Zufallszahlengeneratoren
|
|
29 |
http://eprint.iacr.org/2011/007.pdf (KISS is not cryptographically secure)
|
|
30 |
|
|
31 |
[see also:]
|
3390
|
32 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
|
3397
|
33 |
RandomGenerator - the default; uses the machine's /dev/random if available
|
3356
|
34 |
Random - fast, but generates less quality random numbers
|
|
35 |
RandomTT800 - another random generator
|
|
36 |
RandomParkMiller - another random generator
|
|
37 |
RandomMT19937 - another random generator
|
|
38 |
|
|
39 |
[author:]
|
|
40 |
Ported to Smalltalk by Claus Gittinger.
|
|
41 |
"
|
|
42 |
! !
|
|
43 |
|
|
44 |
!RandomKISS class methodsFor:'instance creation'!
|
|
45 |
|
|
46 |
new
|
|
47 |
^ self basicNew
|
|
48 |
initialize;
|
3384
|
49 |
seed:(Random randomSeed bitAnd:16rFFFFFFFF)
|
3356
|
50 |
!
|
|
51 |
|
|
52 |
new:seed
|
|
53 |
^ self basicNew
|
|
54 |
initialize;
|
|
55 |
seed:seed
|
|
56 |
! !
|
|
57 |
|
|
58 |
!RandomKISS methodsFor:'initialization'!
|
|
59 |
|
|
60 |
initialize
|
|
61 |
x := 123456789. "/ <- any seed !!= 0
|
|
62 |
y := 362436000.
|
|
63 |
z := 521288629.
|
|
64 |
c := 7654321.
|
|
65 |
!
|
|
66 |
|
|
67 |
seed:seed
|
|
68 |
self assert:(seed ~= 0).
|
|
69 |
x := seed.
|
|
70 |
! !
|
|
71 |
|
|
72 |
!RandomKISS methodsFor:'random numbers'!
|
|
73 |
|
|
74 |
nextBoolean
|
3419
|
75 |
"generates a boolean random"
|
3356
|
76 |
|
|
77 |
^ self nextInteger > 16r7FFFFFFF
|
|
78 |
!
|
|
79 |
|
|
80 |
nextInteger
|
|
81 |
"generates the next integer in 0..FFFFFFFF"
|
|
82 |
|
|
83 |
|t|
|
|
84 |
|
|
85 |
"/ Linear Congruencegenerator
|
3364
|
86 |
x := (69069 * x) + 12345.
|
3356
|
87 |
x := x bitAnd:16rFFFFFFFF.
|
|
88 |
|
|
89 |
"/ Xorshift
|
3364
|
90 |
y := y bitXor:(y << 13).
|
3356
|
91 |
y := y bitXor:(y >> 17).
|
|
92 |
y := y bitXor:(y << 5).
|
|
93 |
y := y bitAnd:16rFFFFFFFF.
|
|
94 |
|
|
95 |
"/ Multiply-with-carry
|
3364
|
96 |
t := (698769069 * z) + c.
|
3356
|
97 |
c := t >> 32.
|
|
98 |
z := t bitAnd:16rFFFFFFFF.
|
|
99 |
|
|
100 |
^ (x + y + z) bitAnd:16rFFFFFFFF
|
|
101 |
! !
|
|
102 |
|
|
103 |
!RandomKISS class methodsFor:'documentation'!
|
|
104 |
|
|
105 |
version
|
3419
|
106 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomKISS.st,v 1.7 2014-10-02 16:23:23 cg Exp $'
|
3356
|
107 |
!
|
|
108 |
|
|
109 |
version_CVS
|
3419
|
110 |
^ '$Header: /cvs/stx/stx/libbasic2/RandomKISS.st,v 1.7 2014-10-02 16:23:23 cg Exp $'
|
3356
|
111 |
! !
|
|
112 |
|