class: Random
authorStefan Vogel <sv@exept.de>
Mon, 03 Mar 2014 15:17:39 +0100
changeset 3192 fcff2fefb444
parent 3191 4e04c13386cc
child 3193 2b95268110a2
class: Random changed: #bucketTest: #chiSquareTest
Random.st
--- a/Random.st	Sun Mar 02 01:48:34 2014 +0100
+++ b/Random.st	Mon Mar 03 15:17:39 2014 +0100
@@ -274,8 +274,7 @@
     | nbuckets buckets ntrys slot |
 
     nbuckets := 20.
-    buckets := Array new: nbuckets.
-    buckets atAllPut: 0.
+    buckets := Array new: nbuckets withAll:0.
     ntrys :=  1000.
     ntrys*nbuckets timesRepeat: [
             slot := (randy next * nbuckets) floor + 1.
@@ -286,9 +285,9 @@
 
 
     "Execute this:  
-     Random bucketTest: Random new
+         self bucketTest: self new
+         self bucketTest: RandomGenerator new
     "
-
 !
 
 chiSquareTest   
@@ -298,35 +297,42 @@
             o Random number generator 'passes' if chisquare value is very close to r
             o Repeat test several times, since it may be *wrong* 1 out of 10 trials."
 
-    | aRand frequencies n range t chisquare |
+    | aGenerator frequencies n range t |
+
+    aGenerator := self new.  "Seeded differently each time (if seeded at all)"
+    range := 100.   
+    n := 10000.
+    frequencies := Array new:range withAll:0.
 
-    chisquare := Array new: 10.     "Collect results from 10 trails"
-    1 to: 10 do: [:k |       "k = trail number"
-        aRand := Random new.  "Seeded differently each time"
-        range := 100.   
-        n := 1000.
-        frequencies := Array new: range.
-        1 to: frequencies size do: [ :i | frequencies at: i put: 0 ].
-        1 to: n do: [ :i |
-                t := ((aRand next) * range) truncated.
-                frequencies at: (t+1) put: ((frequencies at: (t + 1)) + 1) ].
-        t := 0.
-        1 to: range do: [ :i |
-                t := t +  ((frequencies at: i) squared) ].
-        chisquare at: k put: (((range * t  / n) - n) asFloat).
+    1 to: n do: [:i |
+        t := ((aGenerator next) * range) truncated + 1.
+        frequencies at:t put: ((frequencies at:t) + 1).
     ].
-    ^ chisquare
+    t := frequencies inject:0 into: [:nextValue :eachFreq |
+            nextValue + eachFreq squared
+        ].
+    ^ ((range * t  / n) - n) asFloat.
 
     "
-     Random chiSquareTest 
+     self chiSquareTest 
+     RandomGenerator chiSquareTest
+    "
+
+   "
+    |fail|
+    fail := 0.
+    10 timesRepeat:[
+        |testResult|
+        testResult := RandomGenerator chiSquareTest.
+        (100 - testResult) abs > 20 ifTrue:[Transcript showCR:testResult. fail := fail + 1].
+    ].
+    fail > 1 ifTrue:[self error:'test failed'].
     "
 
     "
       Sedgewick claims each chisquare number should be 100 +- 20. 
       The closer to 100, the better.
     "
-
-    "Modified: 16.4.1997 / 16:48:26 / cg"
 ! !
 
 !Random methodsFor:'Compatibility-Squeak'!
@@ -699,10 +705,10 @@
 !Random class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.49 2014-03-01 17:56:28 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.49 2014-03-01 17:56:28 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Random.st,v 1.50 2014-03-03 14:17:39 stefan Exp $'
 ! !