|
1 "{ Package: 'stx:libbasic2' }" |
|
2 |
|
3 Object subclass:#RandomBlumBlumShub |
|
4 instanceVariableNames:'p q m seed' |
|
5 classVariableNames:'' |
|
6 poolDictionaries:'' |
|
7 category:'Magnitude-Numbers' |
|
8 ! |
|
9 |
|
10 !RandomBlumBlumShub class methodsFor:'documentation'! |
|
11 |
|
12 documentation |
|
13 " |
|
14 NO WARRANTY |
|
15 |
|
16 This generator uses the blum blub shub algorithm to generate random numbers. |
|
17 It is very slow, but considered to provide good random numbers. |
|
18 |
|
19 RandomBlumBlumShub new nextInteger |
|
20 |
|
21 [see also:] |
|
22 RandomGenerator - the default; uses the machine's /dev/random if available |
|
23 Random - fast, but generates less quality random numbers |
|
24 RandomTT800 - another random generator |
|
25 RandomParkMiller - another random generator |
|
26 RandomMT19937 - another random generator |
|
27 RandomKISS - another random generator |
|
28 exept:libcrypt - a library containing more stuff based on hashes and cyphers |
|
29 |
|
30 [author:] |
|
31 Claus Gittinger. |
|
32 " |
|
33 ! ! |
|
34 |
|
35 !RandomBlumBlumShub class methodsFor:'instance creation'! |
|
36 |
|
37 new |
|
38 ^ self basicNew |
|
39 initialize; |
|
40 seed:(Random randomSeed) |
|
41 ! |
|
42 |
|
43 new:seed |
|
44 self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ]. |
|
45 ^ self basicNew |
|
46 initialize; |
|
47 seed:seed |
|
48 ! ! |
|
49 |
|
50 !RandomBlumBlumShub methodsFor:'initialization'! |
|
51 |
|
52 initialize |
|
53 "The two primes, p and q, should both be congruent to 3 (mod 4)" |
|
54 |
|
55 p := 615389388455725613122981570401989286707. |
|
56 q := 8936277569639798554773638405675965349567. |
|
57 m := p * q. |
|
58 |
|
59 "/ (p \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ]. |
|
60 "/ (q \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ]. |
|
61 |
|
62 " |
|
63 self new initialize |
|
64 " |
|
65 ! |
|
66 |
|
67 seed:seedArg |
|
68 seed := seedArg. |
|
69 |
|
70 self assert:(seed < m). |
|
71 self assert:(seed gcd:m) = 1. |
|
72 ! ! |
|
73 |
|
74 !RandomBlumBlumShub methodsFor:'random numbers'! |
|
75 |
|
76 nextBoolean |
|
77 "generates the next random boolean" |
|
78 |
|
79 seed := seed*seed \\ m. |
|
80 ^ seed bitTest:1 |
|
81 ! |
|
82 |
|
83 nextInteger |
|
84 "generates the next integer in 0..FFFFFFFF" |
|
85 |
|
86 |num| |
|
87 |
|
88 num := 0. |
|
89 32 timesRepeat:[ |
|
90 seed := (seed * seed) \\ m. |
|
91 num := (num<<1) + (seed bitAnd:1). |
|
92 ]. |
|
93 ^ num. |
|
94 |
|
95 " |
|
96 self new nextInteger. |
|
97 |
|
98 |r| |
|
99 r := self new |
|
100 100 timesRepeat:[ Transcript showCR:r nextInteger]. |
|
101 " |
|
102 ! ! |
|
103 |
|
104 !RandomBlumBlumShub class methodsFor:'documentation'! |
|
105 |
|
106 version |
|
107 ^ '$Header: /cvs/stx/stx/libbasic2/RandomBlumBlumShub.st,v 1.1 2014-10-02 16:24:30 cg Exp $' |
|
108 ! |
|
109 |
|
110 version_CVS |
|
111 ^ '$Header: /cvs/stx/stx/libbasic2/RandomBlumBlumShub.st,v 1.1 2014-10-02 16:24:30 cg Exp $' |
|
112 ! ! |
|
113 |