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