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