Random.st
changeset 142 7f9b4315058c
parent 112 3e18f2cfe430
child 232 b2a2cc7ff15e
equal deleted inserted replaced
141:2804943fc6f0 142:7f9b4315058c
    21 |
    21 |
    22 ======================================================================
    22 ======================================================================
    23 
    23 
    24 see notice in (Random>>documentation)
    24 see notice in (Random>>documentation)
    25 "
    25 "
    26 
       
    27 'From Smalltalk/X, Version:2.10.4 on 25-feb-1995 at 9:35:41 am'!
       
    28 
    26 
    29 Stream subclass:#Random
    27 Stream subclass:#Random
    30 	 instanceVariableNames:'seed'
    28 	 instanceVariableNames:'seed'
    31 	 classVariableNames:''
    29 	 classVariableNames:''
    32 	 poolDictionaries:''
    30 	 poolDictionaries:''
    61 
    59 
    62 see notice in (Random>>documentation)
    60 see notice in (Random>>documentation)
    63 "
    61 "
    64 !
    62 !
    65 
    63 
    66 version
       
    67     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.12 1995-11-11 15:21:33 cg Exp $'
       
    68 !
       
    69 
       
    70 documentation
    64 documentation
    71 "
    65 "
    72     random numbers - thanks to Steves GNU Smalltalk
    66     random numbers - thanks to Steves GNU Smalltalk
    73 
    67 
    74     Notice: although being included here,
    68     Notice: although being included here,
    91     "return a new random generator"
    85     "return a new random generator"
    92 
    86 
    93     ^self basicNew setSeed
    87     ^self basicNew setSeed
    94 ! !
    88 ! !
    95 
    89 
       
    90 !Random methodsFor:'accessing-reading'!
       
    91 
       
    92 next
       
    93     "return the next random number in the range 0..1"
       
    94 
       
    95     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
    96     ^ self nextInteger / 16r3FFFFFFF asFloat
       
    97 
       
    98     "|r|
       
    99      r := Random new.
       
   100      Transcript showCr:r next.
       
   101      Transcript showCr:r next.
       
   102      Transcript showCr:r next.
       
   103      Transcript showCr:r next.
       
   104     "
       
   105 !
       
   106 
       
   107 nextBetween:start and:stop
       
   108     "return a random between start and stop.
       
   109      claus: the original GNU version has a bug in returning values
       
   110      from the interval [start .. stop+1]"
       
   111 
       
   112     |rnd|
       
   113 
       
   114     rnd := self next.
       
   115     rnd := rnd * (stop asFloat - start asFloat).
       
   116     rnd := rnd + start asFloat.
       
   117     ^ rnd
       
   118 
       
   119     "|r|
       
   120      r := Random new.
       
   121      Transcript showCr:(r nextBetween:1 and:10).
       
   122      Transcript showCr:(r nextBetween:1 and:10).
       
   123      Transcript showCr:(r nextBetween:1 and:10).
       
   124      Transcript showCr:(r nextBetween:1 and:10).
       
   125     "
       
   126 !
       
   127 
       
   128 nextBoolean
       
   129     "return true or false by random"
       
   130 
       
   131     "thanks to Peter Deutsch ..."
       
   132     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
   133     ^ seed < 16r20000000
       
   134 
       
   135     "|r|
       
   136      r := Random new.
       
   137      Transcript showCr:r nextBoolean.
       
   138      Transcript showCr:r nextBoolean.
       
   139      Transcript showCr:r nextBoolean.
       
   140      Transcript showCr:r nextBoolean.
       
   141     "
       
   142 !
       
   143 
       
   144 nextInteger
       
   145     "return the next integral random number,
       
   146      in the range 0 .. 16r3FFFFFFF.
       
   147      From Sedgewick's 'Algorithms', based on Lehmer's method"
       
   148 
       
   149     "the times: is a kludge - times does not convert to LargeInteger on overflow"
       
   150     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
   151     ^ seed
       
   152 
       
   153     "|r|
       
   154      r := Random new.
       
   155      Transcript showCr:r nextInteger.
       
   156      Transcript showCr:r nextInteger.
       
   157      Transcript showCr:r nextInteger.
       
   158      Transcript showCr:r nextInteger.
       
   159     "
       
   160 !
       
   161 
       
   162 nextIntegerBetween:start and:stop
       
   163     "return an integral random between start and stop"
       
   164 
       
   165     |rnd|
       
   166 
       
   167     rnd := self next.
       
   168     rnd := rnd * (stop asFloat - start asFloat + 1.0).
       
   169     ^ (rnd + start) truncated.
       
   170 
       
   171     "|r|
       
   172      r := Random new.
       
   173      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   174      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   175      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   176      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   177     "
       
   178 !
       
   179 
       
   180 nextMatchFor: aNumber
       
   181     "generate the next random, return true iff it has the same
       
   182      value as aNumber. Redefined to avoid endless reading."
       
   183 
       
   184     ^self next = aNumber
       
   185 ! !
       
   186 
       
   187 !Random methodsFor:'blocked methods'!
       
   188 
       
   189 contents
       
   190     "blocked from use - contents makes no sense for random generators"
       
   191 
       
   192     self shouldNotImplement
       
   193 !
       
   194 
       
   195 nextPut: value
       
   196     "blocked from use - it makes no sense for randoms"
       
   197 
       
   198     self shouldNotImplement
       
   199 ! !
       
   200 
       
   201 !Random methodsFor:'private'!
       
   202 
       
   203 setSeed
       
   204     "set the initial seed value based on the current time"
       
   205 
       
   206     seed := Time secondClock bitAnd: 16r3FFFFFFF
       
   207 ! !
       
   208 
    96 !Random methodsFor:'testing'!
   209 !Random methodsFor:'testing'!
       
   210 
       
   211 atEnd
       
   212     "instances of Random can always give more numbers"
       
   213 
       
   214     ^ false
       
   215 !
    97 
   216 
    98 chiSquare
   217 chiSquare
    99     "perform a chiSquare-test on the receiver"
   218     "perform a chiSquare-test on the receiver"
   100 
   219 
   101     "returns on Sun3 93.40000000000009"
   220     "returns on Sun3 93.40000000000009"
   118     ^r asFloat * t / n - n
   237     ^r asFloat * t / n - n
   119 !
   238 !
   120 
   239 
   121 isReadable
   240 isReadable
   122     ^ true
   241     ^ true
   123 !
   242 ! !
   124 
   243 
   125 atEnd
   244 !Random class methodsFor:'documentation'!
   126     "instances of Random can always give more numbers"
   245 
   127 
   246 version
   128     ^ false
   247     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.13 1995-12-07 21:36:14 cg Exp $'
   129 ! !
   248 ! !
   130 
       
   131 !Random methodsFor:'accessing-reading'!
       
   132 
       
   133 nextInteger
       
   134     "return the next integral random number,
       
   135      in the range 0 .. 16r3FFFFFFF.
       
   136      From Sedgewick's 'Algorithms', based on Lehmer's method"
       
   137 
       
   138     "the times: is a kludge - times does not convert to LargeInteger on overflow"
       
   139     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
   140     ^ seed
       
   141 
       
   142     "|r|
       
   143      r := Random new.
       
   144      Transcript showCr:r nextInteger.
       
   145      Transcript showCr:r nextInteger.
       
   146      Transcript showCr:r nextInteger.
       
   147      Transcript showCr:r nextInteger.
       
   148     "
       
   149 !
       
   150 
       
   151 nextBetween:start and:stop
       
   152     "return a random between start and stop.
       
   153      claus: the original GNU version has a bug in returning values
       
   154      from the interval [start .. stop+1]"
       
   155 
       
   156     |rnd|
       
   157 
       
   158     rnd := self next.
       
   159     rnd := rnd * (stop asFloat - start asFloat).
       
   160     rnd := rnd + start asFloat.
       
   161     ^ rnd
       
   162 
       
   163     "|r|
       
   164      r := Random new.
       
   165      Transcript showCr:(r nextBetween:1 and:10).
       
   166      Transcript showCr:(r nextBetween:1 and:10).
       
   167      Transcript showCr:(r nextBetween:1 and:10).
       
   168      Transcript showCr:(r nextBetween:1 and:10).
       
   169     "
       
   170 !
       
   171 
       
   172 next
       
   173     "return the next random number in the range 0..1"
       
   174 
       
   175     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
   176     ^ self nextInteger / 16r3FFFFFFF asFloat
       
   177 
       
   178     "|r|
       
   179      r := Random new.
       
   180      Transcript showCr:r next.
       
   181      Transcript showCr:r next.
       
   182      Transcript showCr:r next.
       
   183      Transcript showCr:r next.
       
   184     "
       
   185 !
       
   186 
       
   187 nextBoolean
       
   188     "return true or false by random"
       
   189 
       
   190     "thanks to Peter Deutsch ..."
       
   191     seed := (seed times:31415821) + 1 bitAnd: 16r3FFFFFFF.
       
   192     ^ seed < 16r20000000
       
   193 
       
   194     "|r|
       
   195      r := Random new.
       
   196      Transcript showCr:r nextBoolean.
       
   197      Transcript showCr:r nextBoolean.
       
   198      Transcript showCr:r nextBoolean.
       
   199      Transcript showCr:r nextBoolean.
       
   200     "
       
   201 !
       
   202 
       
   203 nextIntegerBetween:start and:stop
       
   204     "return an integral random between start and stop"
       
   205 
       
   206     |rnd|
       
   207 
       
   208     rnd := self next.
       
   209     rnd := rnd * (stop asFloat - start asFloat + 1.0).
       
   210     ^ (rnd + start) truncated.
       
   211 
       
   212     "|r|
       
   213      r := Random new.
       
   214      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   215      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   216      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   217      Transcript showCr:(r nextIntegerBetween:1 and:10).
       
   218     "
       
   219 !
       
   220 
       
   221 nextMatchFor: aNumber
       
   222     "generate the next random, return true iff it has the same
       
   223      value as aNumber. Redefined to avoid endless reading."
       
   224 
       
   225     ^self next = aNumber
       
   226 ! !
       
   227 
       
   228 !Random methodsFor:'blocked methods'!
       
   229 
       
   230 contents
       
   231     "blocked from use - contents makes no sense for random generators"
       
   232 
       
   233     self shouldNotImplement
       
   234 !
       
   235 
       
   236 nextPut: value
       
   237     "blocked from use - it makes no sense for randoms"
       
   238 
       
   239     self shouldNotImplement
       
   240 ! !
       
   241 
       
   242 !Random methodsFor:'private'!
       
   243 
       
   244 setSeed
       
   245     "set the initial seed value based on the current time"
       
   246 
       
   247     seed := Time secondClock bitAnd: 16r3FFFFFFF
       
   248 ! !
       
   249