3366
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
4323
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
3366
|
5 |
Object subclass:#RandomKISS2
|
|
6 |
instanceVariableNames:'x y z c'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
4594
|
9 |
category:'Magnitude-Numbers-Random'
|
3366
|
10 |
!
|
|
11 |
|
|
12 |
!RandomKISS2 class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
|
16 |
Warning: this generator should not be used for cryptographic work.
|
|
17 |
|
|
18 |
NO WARRANTY
|
|
19 |
|
4323
|
20 |
KISS combines a linear congruence, XorShift and Lag with carry generator.
|
3366
|
21 |
The idea is from George Marsaglia.
|
|
22 |
KISS2 uses slightly different parameters and initial state.
|
|
23 |
|
|
24 |
RandomKISS2 new nextInteger
|
|
25 |
(RandomKISS2 new:5489) nextInteger
|
|
26 |
|
|
27 |
If heavily used, it may be useful to embed the original C code as inline C code for a big speedup)
|
|
28 |
|
|
29 |
Please read:
|
|
30 |
http://www.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
|
|
31 |
|
|
32 |
[see also:]
|
3400
|
33 |
RandomGenerator - the default; uses the machine's /dev/random if available
|
3366
|
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 |
!RandomKISS2 class methodsFor:'instance creation'!
|
|
45 |
|
|
46 |
new
|
|
47 |
^ self basicNew
|
|
48 |
initialize;
|
3386
|
49 |
seed:(Random randomSeed bitAnd:16rFFFFFFFF)
|
3366
|
50 |
!
|
|
51 |
|
|
52 |
new:seed
|
|
53 |
^ self basicNew
|
|
54 |
initialize;
|
|
55 |
seed:seed
|
|
56 |
! !
|
|
57 |
|
|
58 |
!RandomKISS2 methodsFor:'initialization'!
|
|
59 |
|
|
60 |
initialize
|
|
61 |
x := 123456789. "/ <- any seed !!= 0
|
3382
|
62 |
y := 987654321.
|
|
63 |
z := 43219876.
|
|
64 |
c := 6543217.
|
3366
|
65 |
!
|
|
66 |
|
|
67 |
seed:seed
|
|
68 |
self assert:(seed ~= 0).
|
|
69 |
x := seed.
|
|
70 |
! !
|
|
71 |
|
|
72 |
!RandomKISS2 methodsFor:'random numbers'!
|
|
73 |
|
|
74 |
nextBoolean
|
3418
|
75 |
"generates a boolean random"
|
3366
|
76 |
|
|
77 |
^ self nextInteger > 16r7FFFFFFF
|
|
78 |
!
|
|
79 |
|
|
80 |
nextInteger
|
|
81 |
"generates the next integer in 0..FFFFFFFF"
|
|
82 |
|
|
83 |
|t|
|
|
84 |
|
|
85 |
"/ Linear Congruencegenerator
|
|
86 |
x := (314527869 * x) + 1234567.
|
|
87 |
x := x bitAnd:16rFFFFFFFF.
|
|
88 |
|
|
89 |
"/ Xorshift
|
|
90 |
y := y bitXor:(y << 5).
|
|
91 |
y := y bitAnd:16rFFFFFFFF.
|
|
92 |
y := y bitXor:(y >> 7).
|
|
93 |
y := y bitAnd:16rFFFFFFFF.
|
|
94 |
y := y bitXor:(y << 22).
|
|
95 |
y := y bitAnd:16rFFFFFFFF.
|
|
96 |
|
|
97 |
"/ Multiply-with-carry
|
|
98 |
t := (4294584393 * z) + c.
|
|
99 |
c := t >> 32.
|
|
100 |
z := t bitAnd:16rFFFFFFFF.
|
|
101 |
|
|
102 |
^ (x + y + z) bitAnd:16rFFFFFFFF
|
|
103 |
! !
|
|
104 |
|
|
105 |
!RandomKISS2 class methodsFor:'documentation'!
|
|
106 |
|
|
107 |
version
|
4323
|
108 |
^ '$Header$'
|
3366
|
109 |
!
|
|
110 |
|
|
111 |
version_CVS
|
4323
|
112 |
^ '$Header$'
|
3366
|
113 |
! !
|
|
114 |
|