5449
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4325
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2014 Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
3421
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
4325
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
3421
|
18 |
Object subclass:#RandomBlumBlumShub
|
|
19 |
instanceVariableNames:'p q m seed'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
4596
|
22 |
category:'Magnitude-Numbers-Random'
|
3421
|
23 |
!
|
|
24 |
|
|
25 |
!RandomBlumBlumShub class methodsFor:'documentation'!
|
|
26 |
|
4325
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2014 Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
3421
|
41 |
documentation
|
|
42 |
"
|
|
43 |
NO WARRANTY
|
|
44 |
|
|
45 |
This generator uses the blum blub shub algorithm to generate random numbers.
|
|
46 |
It is very slow, but considered to provide good random numbers.
|
|
47 |
|
|
48 |
RandomBlumBlumShub new nextInteger
|
|
49 |
|
|
50 |
[see also:]
|
|
51 |
RandomGenerator - the default; uses the machine's /dev/random if available
|
|
52 |
Random - fast, but generates less quality random numbers
|
|
53 |
RandomTT800 - another random generator
|
|
54 |
RandomParkMiller - another random generator
|
|
55 |
RandomMT19937 - another random generator
|
|
56 |
RandomKISS - another random generator
|
|
57 |
exept:libcrypt - a library containing more stuff based on hashes and cyphers
|
|
58 |
|
|
59 |
[author:]
|
|
60 |
Claus Gittinger.
|
|
61 |
"
|
|
62 |
! !
|
|
63 |
|
|
64 |
!RandomBlumBlumShub class methodsFor:'instance creation'!
|
|
65 |
|
|
66 |
new
|
5449
|
67 |
^ self new:(Random randomSeed)
|
3421
|
68 |
!
|
|
69 |
|
|
70 |
new:seed
|
5449
|
71 |
^ self basicNew initialize; seed:seed
|
3421
|
72 |
! !
|
|
73 |
|
|
74 |
!RandomBlumBlumShub methodsFor:'initialization'!
|
|
75 |
|
|
76 |
initialize
|
|
77 |
"The two primes, p and q, should both be congruent to 3 (mod 4)"
|
|
78 |
|
|
79 |
p := 615389388455725613122981570401989286707.
|
|
80 |
q := 8936277569639798554773638405675965349567.
|
|
81 |
m := p * q.
|
|
82 |
|
|
83 |
"/ (p \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ].
|
|
84 |
"/ (q \\ 4) = 3 ifFalse:[ self halt: 'unsuitable p' ].
|
|
85 |
|
|
86 |
"
|
|
87 |
self new initialize
|
|
88 |
"
|
|
89 |
!
|
|
90 |
|
|
91 |
seed:seedArg
|
|
92 |
seed := seedArg.
|
|
93 |
|
|
94 |
self assert:(seed < m).
|
|
95 |
self assert:(seed gcd:m) = 1.
|
|
96 |
! !
|
|
97 |
|
|
98 |
!RandomBlumBlumShub methodsFor:'random numbers'!
|
|
99 |
|
|
100 |
nextBoolean
|
|
101 |
"generates the next random boolean"
|
|
102 |
|
|
103 |
seed := seed*seed \\ m.
|
|
104 |
^ seed bitTest:1
|
|
105 |
!
|
|
106 |
|
|
107 |
nextInteger
|
|
108 |
"generates the next integer in 0..FFFFFFFF"
|
|
109 |
|
|
110 |
|num|
|
|
111 |
|
|
112 |
num := 0.
|
|
113 |
32 timesRepeat:[
|
|
114 |
seed := (seed * seed) \\ m.
|
|
115 |
num := (num<<1) + (seed bitAnd:1).
|
|
116 |
].
|
|
117 |
^ num.
|
|
118 |
|
|
119 |
"
|
|
120 |
self new nextInteger.
|
|
121 |
|
|
122 |
|r|
|
|
123 |
r := self new
|
|
124 |
100 timesRepeat:[ Transcript showCR:r nextInteger].
|
|
125 |
"
|
|
126 |
! !
|
|
127 |
|
|
128 |
!RandomBlumBlumShub class methodsFor:'documentation'!
|
|
129 |
|
|
130 |
version
|
4325
|
131 |
^ '$Header$'
|
3421
|
132 |
!
|
|
133 |
|
|
134 |
version_CVS
|
4325
|
135 |
^ '$Header$'
|
3421
|
136 |
! !
|
|
137 |
|