author | Claus Gittinger <cg@exept.de> |
Tue, 01 Apr 1997 23:09:08 +0200 | |
changeset 517 | 4e47b198c9bc |
parent 350 | 93d5932c76e6 |
child 518 | f5fe8b630e4c |
permissions | -rw-r--r-- |
65 | 1 |
" |
2 |
====================================================================== |
|
0 | 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 |
| |
|
65 | 22 |
====================================================================== |
0 | 23 |
|
65 | 24 |
see notice in (Random>>documentation) |
0 | 25 |
" |
26 |
||
27 |
Stream subclass:#Random |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
28 |
instanceVariableNames:'seed increment multiplier modulus' |
232 | 29 |
classVariableNames:'' |
30 |
poolDictionaries:'' |
|
31 |
category:'Magnitude-Numbers' |
|
0 | 32 |
! |
33 |
||
65 | 34 |
!Random class methodsFor:'documentation'! |
0 | 35 |
|
31 | 36 |
copyright |
37 |
" |
|
38 |
====================================================================== |
|
39 |
| |
|
40 |
| Copyright (C) 1988, 1989 Free Software Foundation, Inc. |
|
41 |
| Written by Steve Byrne. |
|
42 |
| |
|
43 |
| This file is part of GNU Smalltalk. |
|
44 |
| |
|
45 |
| GNU Smalltalk is free software; you can redistribute it and/or modify it |
|
46 |
| under the terms of the GNU General Public License as published by the Free |
|
47 |
| Software Foundation; either version 1, or (at your option) any later version. |
|
48 |
| |
|
49 |
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT |
|
50 |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
51 |
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
52 |
| details. |
|
53 |
| |
|
54 |
| You should have received a copy of the GNU General Public License along with |
|
55 |
| GNU Smalltalk; see the file LICENSE. If not, write to the Free Software |
|
56 |
| Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
57 |
| |
|
58 |
====================================================================== |
|
59 |
||
60 |
see notice in (Random>>documentation) |
|
61 |
" |
|
62 |
! |
|
63 |
||
64 |
documentation |
|
65 |
" |
|
66 |
random numbers - thanks to Steves GNU Smalltalk |
|
67 |
||
68 |
Notice: although being included here, |
|
258 | 69 |
this file is NOT covered by the ST/X license, but by |
70 |
the FSF copyLeft (see copyright method). |
|
31 | 71 |
|
258 | 72 |
You can redistribute it under the terms stated there ... |
73 |
Also, the price you pay for ST/X does not include a charge for |
|
74 |
this file - it has to be considered as a separate piece of |
|
75 |
software, which can be copied and given away without any |
|
76 |
restriction from my (CG) side. |
|
75 | 77 |
|
78 |
claus: the algorithm may need a rewrite for better numbers |
|
258 | 79 |
|
80 |
[author:] |
|
271 | 81 |
Steve Byrne |
258 | 82 |
Claus Gittinger |
271 | 83 |
" |
84 |
! |
|
85 |
||
86 |
examples |
|
87 |
" |
|
88 |
[exBegin] |
|
89 |
|rnd| |
|
258 | 90 |
|
271 | 91 |
rnd := Random new. |
92 |
10 timesRepeat:[ |
|
350 | 93 |
Transcript showCR:(rnd next) |
271 | 94 |
] |
95 |
[exEnd] |
|
96 |
||
97 |
rolling a dice: |
|
98 |
[exBegin] |
|
99 |
|rnd| |
|
100 |
||
101 |
rnd := Random new. |
|
102 |
10 timesRepeat:[ |
|
350 | 103 |
Transcript showCR:(rnd nextIntegerBetween:1 and:6) |
271 | 104 |
] |
105 |
[exEnd] |
|
31 | 106 |
" |
107 |
! ! |
|
0 | 108 |
|
65 | 109 |
!Random class methodsFor:'instance creation'! |
0 | 110 |
|
111 |
new |
|
17 | 112 |
"return a new random generator" |
113 |
||
0 | 114 |
^self basicNew setSeed |
115 |
! ! |
|
116 |
||
65 | 117 |
!Random methodsFor:'accessing-reading'! |
0 | 118 |
|
142 | 119 |
next |
120 |
"return the next random number in the range 0..1" |
|
17 | 121 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
122 |
^ self nextInteger / modulus asFloat |
17 | 123 |
|
124 |
"|r| |
|
125 |
r := Random new. |
|
350 | 126 |
Transcript showCR:r next. |
127 |
Transcript showCR:r next. |
|
128 |
Transcript showCR:r next. |
|
129 |
Transcript showCR:r next. |
|
17 | 130 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
131 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
132 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 133 |
! |
134 |
||
65 | 135 |
nextBetween:start and:stop |
136 |
"return a random between start and stop. |
|
137 |
claus: the original GNU version has a bug in returning values |
|
138 |
from the interval [start .. stop+1]" |
|
139 |
||
140 |
|rnd| |
|
141 |
||
142 |
rnd := self next. |
|
143 |
rnd := rnd * (stop asFloat - start asFloat). |
|
144 |
rnd := rnd + start asFloat. |
|
145 |
^ rnd |
|
146 |
||
147 |
"|r| |
|
148 |
r := Random new. |
|
350 | 149 |
Transcript showCR:(r nextBetween:1 and:10). |
150 |
Transcript showCR:(r nextBetween:1 and:10). |
|
151 |
Transcript showCR:(r nextBetween:1 and:10). |
|
152 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 153 |
" |
154 |
! |
|
155 |
||
0 | 156 |
nextBoolean |
17 | 157 |
"return true or false by random" |
158 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
159 |
^ self nextInteger < (modulus / 2) |
17 | 160 |
|
161 |
"|r| |
|
162 |
r := Random new. |
|
350 | 163 |
Transcript showCR:r nextBoolean. |
164 |
Transcript showCR:r nextBoolean. |
|
165 |
Transcript showCR:r nextBoolean. |
|
166 |
Transcript showCR:r nextBoolean. |
|
17 | 167 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
168 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
169 |
"Modified: 1.4.1997 / 22:52:19 / cg" |
0 | 170 |
! |
171 |
||
142 | 172 |
nextInteger |
173 |
"return the next integral random number, |
|
174 |
in the range 0 .. 16r3FFFFFFF. |
|
175 |
From Sedgewick's 'Algorithms', based on Lehmer's method" |
|
176 |
||
177 |
"the times: is a kludge - times does not convert to LargeInteger on overflow" |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
178 |
self step. |
142 | 179 |
^ seed |
180 |
||
181 |
"|r| |
|
182 |
r := Random new. |
|
350 | 183 |
Transcript showCR:r nextInteger. |
184 |
Transcript showCR:r nextInteger. |
|
185 |
Transcript showCR:r nextInteger. |
|
186 |
Transcript showCR:r nextInteger. |
|
142 | 187 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
188 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
189 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 190 |
! |
191 |
||
17 | 192 |
nextIntegerBetween:start and:stop |
193 |
"return an integral random between start and stop" |
|
194 |
||
195 |
|rnd| |
|
196 |
||
197 |
rnd := self next. |
|
198 |
rnd := rnd * (stop asFloat - start asFloat + 1.0). |
|
199 |
^ (rnd + start) truncated. |
|
200 |
||
201 |
"|r| |
|
202 |
r := Random new. |
|
350 | 203 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
204 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
205 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
206 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 207 |
" |
0 | 208 |
! |
209 |
||
210 |
nextMatchFor: aNumber |
|
17 | 211 |
"generate the next random, return true iff it has the same |
212 |
value as aNumber. Redefined to avoid endless reading." |
|
213 |
||
0 | 214 |
^self next = aNumber |
215 |
! ! |
|
216 |
||
65 | 217 |
!Random methodsFor:'blocked methods'! |
0 | 218 |
|
65 | 219 |
contents |
220 |
"blocked from use - contents makes no sense for random generators" |
|
221 |
||
222 |
self shouldNotImplement |
|
223 |
! |
|
224 |
||
225 |
nextPut: value |
|
226 |
"blocked from use - it makes no sense for randoms" |
|
227 |
||
228 |
self shouldNotImplement |
|
229 |
! ! |
|
230 |
||
231 |
!Random methodsFor:'private'! |
|
0 | 232 |
|
233 |
setSeed |
|
17 | 234 |
"set the initial seed value based on the current time" |
235 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
236 |
seed := Time millisecondClockValue bitAnd:16rFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
237 |
multiplier := 2311. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
238 |
increment := 25367. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
239 |
modulus := 120050. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
240 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
241 |
"/ seed := Time secondClock bitAnd: 16r3FFFFFFF |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
242 |
"/ multiplier := 31415821. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
243 |
"/ modulus := 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
244 |
"/ increment := 1. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
245 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
246 |
"Modified: 1.4.1997 / 22:44:13 / cg" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
247 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
248 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
249 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
250 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
251 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
252 |
"/ seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
253 |
seed := seed * multiplier + increment \\ modulus |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
254 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
255 |
"Created: 1.4.1997 / 22:40:45 / cg" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
256 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 257 |
! ! |
65 | 258 |
|
142 | 259 |
!Random methodsFor:'testing'! |
260 |
||
261 |
atEnd |
|
262 |
"instances of Random can always give more numbers" |
|
263 |
||
264 |
^ false |
|
265 |
! |
|
266 |
||
267 |
chiSquare |
|
268 |
"perform a chiSquare-test on the receiver" |
|
269 |
||
270 |
"returns on Sun3 93.40000000000009" |
|
271 |
^self chiSquare: 1000 range: 100 |
|
272 |
||
273 |
"Random new chiSquare" |
|
274 |
! |
|
275 |
||
276 |
chiSquare: n range: r |
|
277 |
| f t s | |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
278 |
self setSeed. "/ seed := 1234567. |
142 | 279 |
f := Array new: r + 1. |
280 |
1 to: r + 1 do: [ :i | f at: i put: 0 ]. |
|
281 |
n timesRepeat: |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
282 |
[ self step. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
283 |
t := seed \\ r. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
284 |
f at: t + 1 put: (f at: t + 1) + 1 ]. |
142 | 285 |
t := 0. |
286 |
1 to: r do: [ :i | t := t + (f at: i) squared ]. |
|
287 |
^r asFloat * t / n - n |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
288 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
289 |
"Modified: 1.4.1997 / 22:47:35 / cg" |
142 | 290 |
! |
291 |
||
292 |
isReadable |
|
293 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
294 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
295 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
296 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
297 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
298 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
299 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 300 |
! ! |
301 |
||
302 |
!Random class methodsFor:'documentation'! |
|
303 |
||
304 |
version |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
305 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.18 1997-04-01 21:09:08 cg Exp $' |
142 | 306 |
! ! |