4601
|
1 |
"
|
|
2 |
======================================================================
|
|
3 |
|
|
|
4 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
|
|
5 |
| Written by Steve Byrne.
|
|
6 |
|
|
|
7 |
| This file is part of GNU Smalltalk.
|
|
8 |
|
|
|
9 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it
|
|
10 |
| under the terms of the GNU General Public License as published by the Free
|
|
11 |
| Software Foundation; either version 1, or (at your option) any later version.
|
|
12 |
|
|
|
13 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
|
|
14 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
15 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
16 |
| details.
|
|
17 |
|
|
|
18 |
| You should have received a copy of the GNU General Public License along with
|
|
19 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software
|
|
20 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
21 |
|
|
|
22 |
======================================================================
|
|
23 |
|
|
24 |
see notice in (Random>>documentation)
|
|
25 |
"
|
|
26 |
"{ Package: 'stx:libbasic2' }"
|
|
27 |
|
|
28 |
"{ NameSpace: Smalltalk }"
|
|
29 |
|
|
30 |
Random subclass:#RandomGNUSmalltalk
|
|
31 |
instanceVariableNames:'increment multiplier modulus'
|
|
32 |
classVariableNames:'SharedGenerator'
|
|
33 |
poolDictionaries:''
|
|
34 |
category:'Magnitude-Numbers-Random'
|
|
35 |
!
|
|
36 |
|
|
37 |
!RandomGNUSmalltalk class methodsFor:'documentation'!
|
|
38 |
|
|
39 |
copyright
|
|
40 |
"
|
|
41 |
======================================================================
|
|
42 |
|
|
|
43 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc.
|
|
44 |
| Written by Steve Byrne.
|
|
45 |
|
|
|
46 |
| This file is part of GNU Smalltalk.
|
|
47 |
|
|
|
48 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it
|
|
49 |
| under the terms of the GNU General Public License as published by the Free
|
|
50 |
| Software Foundation; either version 1, or (at your option) any later version.
|
|
51 |
|
|
|
52 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
|
|
53 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
54 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
55 |
| details.
|
|
56 |
|
|
|
57 |
| You should have received a copy of the GNU General Public License along with
|
|
58 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software
|
|
59 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
60 |
|
|
|
61 |
======================================================================
|
|
62 |
|
|
63 |
see notice in (Random>>documentation)
|
|
64 |
"
|
|
65 |
!
|
|
66 |
|
|
67 |
documentation
|
|
68 |
"
|
|
69 |
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
|
|
70 |
=======================================================================================
|
|
71 |
DO NOT USE THIS GENERATOR FOR CRYPTOGRAPHY OR OTHER SECURITY RELATED WORK,
|
|
72 |
because linear congruential generators are predictable and can be broken easily!!
|
|
73 |
|
|
74 |
Smalltalk/X includes a better generator named RandomGenerator,
|
|
75 |
which uses the best available generator of the system
|
|
76 |
(either OS-random, /dev/random or as a less dangerous fallback, an RC4-based random generator).
|
|
77 |
Please use that one.
|
|
78 |
=======================================================================================
|
|
79 |
|
|
80 |
A simple random numbers - thanks to Steves GNU Smalltalk
|
|
81 |
|
|
82 |
This implements a linear congruential maximum period random number generator
|
|
83 |
which passes the spectral test for randomness for dimensions 2 3 4 5 6.
|
|
84 |
|
|
85 |
Notice: although being included here,
|
|
86 |
this file is NOT covered by the ST/X license, but by
|
|
87 |
the FSF copyLeft (see copyright method).
|
|
88 |
|
|
89 |
You can redistribute it under the terms stated there ...
|
|
90 |
Also, the price you pay for ST/X does not include a charge for
|
|
91 |
this file - it has to be considered as a separate piece of
|
|
92 |
software, which can be copied and given away without any
|
|
93 |
restriction from my (CG) side.
|
|
94 |
|
|
95 |
[author:]
|
|
96 |
Steve Byrne
|
|
97 |
Claus Gittinger
|
|
98 |
|
|
99 |
[see also:]
|
|
100 |
http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf
|
|
101 |
RandomTT800 - a new random generator
|
|
102 |
RandomParkMiller - another new random generator
|
|
103 |
"
|
|
104 |
!
|
|
105 |
|
|
106 |
examples
|
|
107 |
"
|
|
108 |
[exBegin]
|
|
109 |
|rnd|
|
|
110 |
|
|
111 |
rnd := RandomGNUSmalltalk new.
|
|
112 |
10 timesRepeat:[
|
|
113 |
Transcript showCR:(rnd next)
|
|
114 |
]
|
|
115 |
[exEnd]
|
|
116 |
|
|
117 |
rolling a dice:
|
|
118 |
[exBegin]
|
|
119 |
|rnd|
|
|
120 |
|
|
121 |
rnd := RandomGNUSmalltalk new.
|
|
122 |
10 timesRepeat:[
|
|
123 |
Transcript showCR:(rnd nextIntegerBetween:1 and:6)
|
|
124 |
]
|
|
125 |
[exEnd]
|
|
126 |
"
|
|
127 |
! !
|
|
128 |
|
|
129 |
!RandomGNUSmalltalk methodsFor:'accessing-reading'!
|
|
130 |
|
|
131 |
maxInteger
|
|
132 |
^ modulus
|
|
133 |
!
|
|
134 |
|
|
135 |
next
|
|
136 |
"return the next random number in the range ]0..1["
|
|
137 |
|
|
138 |
self step.
|
|
139 |
^ seed / modulus asFloat
|
|
140 |
|
|
141 |
"
|
|
142 |
|r|
|
|
143 |
r := Random new.
|
|
144 |
Transcript showCR:r next.
|
|
145 |
Transcript showCR:r next.
|
|
146 |
Transcript showCR:r next.
|
|
147 |
Transcript showCR:r next.
|
|
148 |
"
|
|
149 |
|
|
150 |
"Modified: 1.4.1997 / 22:44:46 / cg"
|
|
151 |
!
|
|
152 |
|
|
153 |
nextBoolean
|
|
154 |
"return true or false by random"
|
|
155 |
|
|
156 |
self step.
|
|
157 |
^ seed < (modulus // 2)
|
|
158 |
|
|
159 |
"
|
|
160 |
|r|
|
|
161 |
r := Random new.
|
|
162 |
Transcript showCR:r nextBoolean.
|
|
163 |
Transcript showCR:r nextBoolean.
|
|
164 |
Transcript showCR:r nextBoolean.
|
|
165 |
Transcript showCR:r nextBoolean.
|
|
166 |
"
|
|
167 |
|
|
168 |
"
|
|
169 |
|r bag|
|
|
170 |
r := Random new.
|
|
171 |
bag := Bag new.
|
|
172 |
1000000 timesRepeat:[
|
|
173 |
bag add:(r nextBoolean).
|
|
174 |
].
|
|
175 |
Transcript showCR:bag contents
|
|
176 |
"
|
|
177 |
|
|
178 |
"Modified: / 22-10-2008 / 15:17:57 / cg"
|
|
179 |
!
|
|
180 |
|
|
181 |
nextInteger
|
|
182 |
"return the next integral random number,
|
|
183 |
in the range 0 .. modulus (which is less than 16r3FFFFFFF).
|
|
184 |
From Sedgewick's 'Algorithms', based on Lehmer's method.
|
|
185 |
|
|
186 |
Take care, this returns an even number after each odd number!!"
|
|
187 |
|
|
188 |
self step.
|
|
189 |
^ seed
|
|
190 |
|
|
191 |
"
|
|
192 |
|r|
|
|
193 |
r := Random new.
|
|
194 |
Transcript showCR:r nextInteger.
|
|
195 |
Transcript showCR:r nextInteger.
|
|
196 |
Transcript showCR:r nextInteger.
|
|
197 |
Transcript showCR:r nextInteger.
|
|
198 |
"
|
|
199 |
|
|
200 |
"Modified: 1.4.1997 / 22:42:53 / cg"
|
|
201 |
! !
|
|
202 |
|
|
203 |
!RandomGNUSmalltalk methodsFor:'private'!
|
|
204 |
|
|
205 |
setSeed:seedValue
|
|
206 |
"set the initial seed and intialite the PRNG parameters.
|
|
207 |
These numbers implement a maximum period generator which passes
|
|
208 |
the spectral test for randomness for dimensions 2 3 4 5 6 and
|
|
209 |
the product does not overflow 2 raisedTo:29.
|
|
210 |
|
|
211 |
These numbers are carefully choosen, so don't change them,
|
|
212 |
unless you know what you are doing!!"
|
|
213 |
|
|
214 |
modulus := 244944 " 244957 " .
|
|
215 |
multiplier := 1597.
|
|
216 |
increment := 51749.
|
|
217 |
seed := seedValue \\ modulus.
|
|
218 |
|
|
219 |
self step.
|
|
220 |
|
|
221 |
"Modified: / 12-11-1999 / 17:50:52 / stefan"
|
|
222 |
"Created: / 26-05-2007 / 21:25:10 / cg"
|
|
223 |
!
|
|
224 |
|
|
225 |
step
|
|
226 |
"compute the next random integer"
|
|
227 |
|
|
228 |
seed := (seed * multiplier + increment) \\ modulus
|
|
229 |
|
|
230 |
"Created: 1.4.1997 / 22:40:45 / cg"
|
|
231 |
"Modified: 1.4.1997 / 22:43:01 / cg"
|
|
232 |
! !
|
|
233 |
|
|
234 |
!RandomGNUSmalltalk class methodsFor:'documentation'!
|
|
235 |
|
|
236 |
version
|
|
237 |
^ '$Header$'
|
|
238 |
!
|
|
239 |
|
|
240 |
version_CVS
|
|
241 |
^ '$Header$'
|
|
242 |
! !
|
|
243 |
|