author | Claus Gittinger <cg@exept.de> |
Thu, 21 Aug 1997 18:10:25 +0200 | |
changeset 561 | f00675b5531f |
parent 518 | f5fe8b630e4c |
child 680 | a7b98fdac3dc |
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' |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
29 |
classVariableNames:'RandomGenerator' |
232 | 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 |
||
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
117 |
!Random class methodsFor:'random numbers'! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
118 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
119 |
nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
120 |
"return a random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
121 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
122 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
123 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
124 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
125 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
126 |
RandomGenerator isNil ifTrue:[ |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
127 |
RandomGenerator := self new. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
128 |
]. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
129 |
^ RandomGenerator nextBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
130 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
131 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
132 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
133 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
134 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
135 |
Transcript showCR:(Random nextBetween:1 and:100). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
136 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
137 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
138 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
139 |
"Created: 21.8.1997 / 18:09:36 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
140 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
141 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
142 |
nextInteger |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
143 |
"return an integral random number. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
144 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
145 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
146 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
147 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
148 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
149 |
RandomGenerator isNil ifTrue:[ |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
150 |
RandomGenerator := self new. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
151 |
]. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
152 |
^ RandomGenerator nextInteger. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
153 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
154 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
155 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
156 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
157 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
158 |
Transcript showCR:(Random nextInteger). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
159 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
160 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
161 |
"Created: 21.8.1997 / 18:08:23 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
162 |
! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
163 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
164 |
nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
165 |
"return an integral random number between start and stop. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
166 |
This method behaves like the corresponding instance method, |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
167 |
but allows generation of random numbers without |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
168 |
a need for an instance of Random to be kept around. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
169 |
This uses a common, shared generator." |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
170 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
171 |
RandomGenerator isNil ifTrue:[ |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
172 |
RandomGenerator := self new. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
173 |
]. |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
174 |
^ RandomGenerator nextIntegerBetween:start and:stop |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
175 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
176 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
177 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
178 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
179 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
180 |
Transcript showCR:(Random nextIntegerBetween:1 and:10). |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
181 |
" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
182 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
183 |
"Created: 21.8.1997 / 18:07:00 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
184 |
"Modified: 21.8.1997 / 18:08:56 / cg" |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
185 |
! ! |
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
186 |
|
518 | 187 |
!Random class methodsFor:'testing'! |
188 |
||
189 |
chiSquareTest |
|
190 |
" Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', |
|
191 |
o N = number of samples |
|
192 |
o r = range of random numners is [0,r) -- condition: N >= 10r. |
|
193 |
o Random number generator 'passes' if chisquare value is very close to r |
|
194 |
o Repeat test several times, since it may be *wrong* 1 out of 10 trials." |
|
195 |
||
196 |
| aRand frequencies n range t chisquare | |
|
197 |
||
198 |
chisquare := Array new: 10. "Collect results from 10 trails" |
|
199 |
1 to: 10 do: [:k | "k = trail number" |
|
200 |
aRand := Random new. "Seeded differently each time" |
|
201 |
range := 100. |
|
202 |
n := 1000. |
|
203 |
frequencies := Array new: range. |
|
204 |
1 to: frequencies size do: [ :i | frequencies at: i put: 0 ]. |
|
205 |
1 to: n do: [ :i | |
|
206 |
t := ((aRand next) * range) truncated. |
|
207 |
frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ]. |
|
208 |
t := 0. |
|
209 |
1 to: range do: [ :i | |
|
210 |
t := t + ((frequencies at: i) squared) ]. |
|
211 |
chisquare at: k put: (((range * t / n) - n) asFloat). |
|
212 |
]. |
|
213 |
^ chisquare |
|
214 |
||
215 |
" |
|
216 |
Random chiSquareTest |
|
217 |
" |
|
218 |
||
219 |
" |
|
220 |
Sedgewick claims each chisquare number should be 100 +- 20. |
|
221 |
The closer to 100, the better. |
|
222 |
" |
|
223 |
||
224 |
"Modified: 16.4.1997 / 16:48:26 / cg" |
|
225 |
! ! |
|
226 |
||
65 | 227 |
!Random methodsFor:'accessing-reading'! |
0 | 228 |
|
142 | 229 |
next |
230 |
"return the next random number in the range 0..1" |
|
17 | 231 |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
232 |
^ self nextInteger / modulus asFloat |
17 | 233 |
|
234 |
"|r| |
|
235 |
r := Random new. |
|
350 | 236 |
Transcript showCR:r next. |
237 |
Transcript showCR:r next. |
|
238 |
Transcript showCR:r next. |
|
239 |
Transcript showCR:r next. |
|
17 | 240 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
241 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
242 |
"Modified: 1.4.1997 / 22:44:46 / cg" |
0 | 243 |
! |
244 |
||
65 | 245 |
nextBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
246 |
"return a random number between start and stop. |
65 | 247 |
claus: the original GNU version has a bug in returning values |
248 |
from the interval [start .. stop+1]" |
|
249 |
||
250 |
|rnd| |
|
251 |
||
252 |
rnd := self next. |
|
253 |
rnd := rnd * (stop asFloat - start asFloat). |
|
254 |
rnd := rnd + start asFloat. |
|
255 |
^ rnd |
|
256 |
||
257 |
"|r| |
|
258 |
r := Random new. |
|
350 | 259 |
Transcript showCR:(r nextBetween:1 and:10). |
260 |
Transcript showCR:(r nextBetween:1 and:10). |
|
261 |
Transcript showCR:(r nextBetween:1 and:10). |
|
262 |
Transcript showCR:(r nextBetween:1 and:10). |
|
65 | 263 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
264 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
265 |
"Modified: 21.8.1997 / 18:10:00 / cg" |
65 | 266 |
! |
267 |
||
0 | 268 |
nextBoolean |
17 | 269 |
"return true or false by random" |
270 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
271 |
^ self nextInteger < (modulus / 2) |
17 | 272 |
|
273 |
"|r| |
|
274 |
r := Random new. |
|
350 | 275 |
Transcript showCR:r nextBoolean. |
276 |
Transcript showCR:r nextBoolean. |
|
277 |
Transcript showCR:r nextBoolean. |
|
278 |
Transcript showCR:r nextBoolean. |
|
17 | 279 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
280 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
281 |
"Modified: 1.4.1997 / 22:52:19 / cg" |
0 | 282 |
! |
283 |
||
142 | 284 |
nextInteger |
285 |
"return the next integral random number, |
|
286 |
in the range 0 .. 16r3FFFFFFF. |
|
287 |
From Sedgewick's 'Algorithms', based on Lehmer's method" |
|
288 |
||
289 |
"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
|
290 |
self step. |
142 | 291 |
^ seed |
292 |
||
293 |
"|r| |
|
294 |
r := Random new. |
|
350 | 295 |
Transcript showCR:r nextInteger. |
296 |
Transcript showCR:r nextInteger. |
|
297 |
Transcript showCR:r nextInteger. |
|
298 |
Transcript showCR:r nextInteger. |
|
142 | 299 |
" |
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
300 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
301 |
"Modified: 1.4.1997 / 22:42:53 / cg" |
142 | 302 |
! |
303 |
||
17 | 304 |
nextIntegerBetween:start and:stop |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
305 |
"return an integral random number between start and stop" |
17 | 306 |
|
307 |
|rnd| |
|
308 |
||
309 |
rnd := self next. |
|
310 |
rnd := rnd * (stop asFloat - start asFloat + 1.0). |
|
311 |
^ (rnd + start) truncated. |
|
312 |
||
313 |
"|r| |
|
314 |
r := Random new. |
|
350 | 315 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
316 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
317 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
318 |
Transcript showCR:(r nextIntegerBetween:1 and:10). |
|
17 | 319 |
" |
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
320 |
|
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
321 |
"Modified: 21.8.1997 / 18:10:08 / cg" |
0 | 322 |
! |
323 |
||
324 |
nextMatchFor: aNumber |
|
17 | 325 |
"generate the next random, return true iff it has the same |
326 |
value as aNumber. Redefined to avoid endless reading." |
|
327 |
||
0 | 328 |
^self next = aNumber |
329 |
! ! |
|
330 |
||
65 | 331 |
!Random methodsFor:'blocked methods'! |
0 | 332 |
|
65 | 333 |
contents |
334 |
"blocked from use - contents makes no sense for random generators" |
|
335 |
||
336 |
self shouldNotImplement |
|
337 |
! |
|
338 |
||
339 |
nextPut: value |
|
340 |
"blocked from use - it makes no sense for randoms" |
|
341 |
||
342 |
self shouldNotImplement |
|
343 |
! ! |
|
344 |
||
345 |
!Random methodsFor:'private'! |
|
0 | 346 |
|
347 |
setSeed |
|
17 | 348 |
"set the initial seed value based on the current time" |
349 |
||
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
350 |
seed := Time millisecondClockValue bitAnd:16rFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
351 |
multiplier := 2311. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
352 |
increment := 25367. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
353 |
modulus := 120050. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
354 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
355 |
"/ seed := Time secondClock bitAnd: 16r3FFFFFFF |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
356 |
"/ multiplier := 31415821. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
357 |
"/ modulus := 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
358 |
"/ increment := 1. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
359 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
360 |
"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
|
361 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
362 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
363 |
step |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
364 |
"compute the next random integer" |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
365 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
366 |
"/ seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF. |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
367 |
seed := seed * multiplier + increment \\ modulus |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
368 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
369 |
"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
|
370 |
"Modified: 1.4.1997 / 22:43:01 / cg" |
0 | 371 |
! ! |
65 | 372 |
|
142 | 373 |
!Random methodsFor:'testing'! |
374 |
||
375 |
atEnd |
|
376 |
"instances of Random can always give more numbers" |
|
377 |
||
378 |
^ false |
|
379 |
! |
|
380 |
||
381 |
isReadable |
|
382 |
^ true |
|
517
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
383 |
! |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
384 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
385 |
isWritable |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
386 |
^ false |
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
387 |
|
4e47b198c9bc
allow multiplier, increment & modulus to be changed.
Claus Gittinger <cg@exept.de>
parents:
350
diff
changeset
|
388 |
"Created: 1.4.1997 / 22:38:27 / cg" |
142 | 389 |
! ! |
390 |
||
391 |
!Random class methodsFor:'documentation'! |
|
392 |
||
393 |
version |
|
561
f00675b5531f
added class protocol for random numbers
Claus Gittinger <cg@exept.de>
parents:
518
diff
changeset
|
394 |
^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.20 1997-08-21 16:10:25 cg Exp $' |
142 | 395 |
! ! |