Random.st
changeset 3192 fcff2fefb444
parent 3188 e22bc4a307e1
child 3353 0a54e2d850aa
equal deleted inserted replaced
3191:4e04c13386cc 3192:fcff2fefb444
   272     "
   272     "
   273 
   273 
   274     | nbuckets buckets ntrys slot |
   274     | nbuckets buckets ntrys slot |
   275 
   275 
   276     nbuckets := 20.
   276     nbuckets := 20.
   277     buckets := Array new: nbuckets.
   277     buckets := Array new: nbuckets withAll:0.
   278     buckets atAllPut: 0.
       
   279     ntrys :=  1000.
   278     ntrys :=  1000.
   280     ntrys*nbuckets timesRepeat: [
   279     ntrys*nbuckets timesRepeat: [
   281             slot := (randy next * nbuckets) floor + 1.
   280             slot := (randy next * nbuckets) floor + 1.
   282             buckets at: slot put: (buckets at: slot) + 1 ].
   281             buckets at: slot put: (buckets at: slot) + 1 ].
   283     Transcript cr.
   282     Transcript cr.
   284     1 to: nbuckets do: [ :nb |
   283     1 to: nbuckets do: [ :nb |
   285             Transcript show: (buckets at: nb) printString, ' ' ]
   284             Transcript show: (buckets at: nb) printString, ' ' ]
   286 
   285 
   287 
   286 
   288     "Execute this:  
   287     "Execute this:  
   289      Random bucketTest: Random new
   288          self bucketTest: self new
   290     "
   289          self bucketTest: RandomGenerator new
   291 
   290     "
   292 !
   291 !
   293 
   292 
   294 chiSquareTest   
   293 chiSquareTest   
   295     " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
   294     " Chi-Squared Test - from R.Sedgewick's 1st ed. of 'Algorithms', 
   296             o N = number of samples
   295             o N = number of samples
   297             o r  = range of random numners is [0,r)      -- condition: N >= 10r.
   296             o r  = range of random numners is [0,r)      -- condition: N >= 10r.
   298             o Random number generator 'passes' if chisquare value is very close to r
   297             o Random number generator 'passes' if chisquare value is very close to r
   299             o Repeat test several times, since it may be *wrong* 1 out of 10 trials."
   298             o Repeat test several times, since it may be *wrong* 1 out of 10 trials."
   300 
   299 
   301     | aRand frequencies n range t chisquare |
   300     | aGenerator frequencies n range t |
   302 
   301 
   303     chisquare := Array new: 10.     "Collect results from 10 trails"
   302     aGenerator := self new.  "Seeded differently each time (if seeded at all)"
   304     1 to: 10 do: [:k |       "k = trail number"
   303     range := 100.   
   305         aRand := Random new.  "Seeded differently each time"
   304     n := 10000.
   306         range := 100.   
   305     frequencies := Array new:range withAll:0.
   307         n := 1000.
   306 
   308         frequencies := Array new: range.
   307     1 to: n do: [:i |
   309         1 to: frequencies size do: [ :i | frequencies at: i put: 0 ].
   308         t := ((aGenerator next) * range) truncated + 1.
   310         1 to: n do: [ :i |
   309         frequencies at:t put: ((frequencies at:t) + 1).
   311                 t := ((aRand next) * range) truncated.
       
   312                 frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ].
       
   313         t := 0.
       
   314         1 to: range do: [ :i |
       
   315                 t := t +  ((frequencies at: i) squared) ].
       
   316         chisquare at: k put: (((range * t  / n) - n) asFloat).
       
   317     ].
   310     ].
   318     ^ chisquare
   311     t := frequencies inject:0 into: [:nextValue :eachFreq |
   319 
   312             nextValue + eachFreq squared
   320     "
   313         ].
   321      Random chiSquareTest 
   314     ^ ((range * t  / n) - n) asFloat.
       
   315 
       
   316     "
       
   317      self chiSquareTest 
       
   318      RandomGenerator chiSquareTest
       
   319     "
       
   320 
       
   321    "
       
   322     |fail|
       
   323     fail := 0.
       
   324     10 timesRepeat:[
       
   325         |testResult|
       
   326         testResult := RandomGenerator chiSquareTest.
       
   327         (100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1].
       
   328     ].
       
   329     fail > 1 ifTrue:[self error:'test failed'].
   322     "
   330     "
   323 
   331 
   324     "
   332     "
   325       Sedgewick claims each chisquare number should be 100 +- 20. 
   333       Sedgewick claims each chisquare number should be 100 +- 20. 
   326       The closer to 100, the better.
   334       The closer to 100, the better.
   327     "
   335     "
   328 
       
   329     "Modified: 16.4.1997 / 16:48:26 / cg"
       
   330 ! !
   336 ! !
   331 
   337 
   332 !Random methodsFor:'Compatibility-Squeak'!
   338 !Random methodsFor:'Compatibility-Squeak'!
   333 
   339 
   334 nextInt:upperBound
   340 nextInt:upperBound
   697 ! !
   703 ! !
   698 
   704 
   699 !Random class methodsFor:'documentation'!
   705 !Random class methodsFor:'documentation'!
   700 
   706 
   701 version
   707 version
   702     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.49 2014-03-01 17:56:28 cg Exp $'
   708     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $'
   703 !
   709 !
   704 
   710 
   705 version_CVS
   711 version_CVS
   706     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.49 2014-03-01 17:56:28 cg Exp $'
   712     ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $'
   707 ! !
   713 ! !
   708 
   714