Random.st
changeset 793 037f3ceb4e0a
parent 680 a7b98fdac3dc
child 840 5f6559515588
equal deleted inserted replaced
792:2d3e914e5ae9 793:037f3ceb4e0a
   183     "Created: 21.8.1997 / 18:07:00 / cg"
   183     "Created: 21.8.1997 / 18:07:00 / cg"
   184     "Modified: 21.8.1997 / 18:08:56 / cg"
   184     "Modified: 21.8.1997 / 18:08:56 / cg"
   185 ! !
   185 ! !
   186 
   186 
   187 !Random class methodsFor:'testing'!
   187 !Random class methodsFor:'testing'!
       
   188 
       
   189 bucketTest: randy
       
   190     "A quick-and-dirty bucket test. Prints nbuckets values on the Transcript.
       
   191      Each should be 'near' the value of ntries. Any run with any value 'far' from ntries
       
   192      indicates something is very wrong. Each run generates different values.
       
   193      For a slightly better test, try values of nbuckets of 200-1000 or more; 
       
   194      go get coffee.
       
   195      This is a poor test; see Knuth.   
       
   196      Some 'OK' runs:
       
   197            1000 1023 998 969 997 1018 1030 1019 1054 985 1003
       
   198            1011 987 982 980 982 974 968 1044 976
       
   199            1029 1011 1025 1016 997 1019 991 954 968 999 991
       
   200            978 1035 995 988 1038 1009 988 993 976
       
   201     "
       
   202 
       
   203     | nbuckets buckets ntrys slot |
       
   204 
       
   205     nbuckets := 20.
       
   206     buckets := Array new: nbuckets.
       
   207     buckets atAllPut: 0.
       
   208     ntrys :=  1000.
       
   209     ntrys*nbuckets timesRepeat: [
       
   210             slot := (randy next * nbuckets) floor + 1.
       
   211             buckets at: slot put: (buckets at: slot) + 1 ].
       
   212     Transcript cr.
       
   213     1 to: nbuckets do: [ :nb |
       
   214             Transcript show: (buckets at: nb) printString, ' ' ]
       
   215 
       
   216 
       
   217     "Execute this:  
       
   218      Random bucketTest: Random new
       
   219     "
       
   220 
       
   221 !
   188 
   222 
   189 chiSquareTest   
   223 chiSquareTest   
   190     " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
   224     " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
   191             o N = number of samples
   225             o N = number of samples
   192             o r  = range of random numners is [0,r)      -- condition: N >= 10r.
   226             o r  = range of random numners is [0,r)      -- condition: N >= 10r.
   294 nextInteger
   328 nextInteger
   295     "return the next integral random number,
   329     "return the next integral random number,
   296      in the range 0 .. 16r3FFFFFFF.
   330      in the range 0 .. 16r3FFFFFFF.
   297      From Sedgewick's 'Algorithms', based on Lehmer's method"
   331      From Sedgewick's 'Algorithms', based on Lehmer's method"
   298 
   332 
   299     "the times: is a kludge - times does not convert to LargeInteger on overflow"
       
   300     self step.
   333     self step.
   301     ^ seed
   334     ^ seed
   302 
   335 
   303     "|r|
   336     "|r|
   304      r := Random new.
   337      r := Random new.
   410 ! !
   443 ! !
   411 
   444 
   412 !Random class methodsFor:'documentation'!
   445 !Random class methodsFor:'documentation'!
   413 
   446 
   414 version
   447 version
   415     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.21 1998-08-21 12:47:38 cg Exp $'
   448     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.22 1999-08-12 09:42:31 cg Exp $'
   416 ! !
   449 ! !