RandomGenerator.st
changeset 4602 b74d89eed13a
parent 4597 42570a22d794
child 5176 55374cc71a4b
--- a/RandomGenerator.st	Fri Mar 02 17:06:06 2018 +0100
+++ b/RandomGenerator.st	Fri Mar 02 17:06:22 2018 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2007 by eXept Software AG
               All Rights Reserved
@@ -115,7 +117,7 @@
     |result|
 
     SharedRandomGenerator notNil ifTrue:[
-        "each time, we do a new, add some entropy to the SharedGenerator"
+        "each time, we do a new, add some entropy to the SharedRandomGenerator"
         SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime.
         ^ SharedRandomGenerator.
     ].
@@ -172,7 +174,7 @@
             SharedRandomGenerator notNil ifTrue:[
                 SharedRandomGenerator := nil.
                 HasOSRandom := RandFile := nil.
-                self new. "/ opens a new sharedGenerator
+                self new. "/ opens a new SharedRandomGenerator
             ]
         ]
     ].
@@ -271,6 +273,22 @@
 
 !RandomGenerator methodsFor:'reading'!
 
+maxInteger
+    "return the max integral random number returned by nextInteger"
+
+    SmallInteger maxBytes == 8 ifTrue:[
+        ^ 16r3FFFFFFFFFFFFFFF
+    ] ifFalse:[
+        ^ 16r3FFFFFFF
+    ].
+
+    "
+     RandomGenerator new maxInteger.
+    "
+
+    "Modified: / 11.11.1999 / 10:08:10 / stefan"
+!
+
 next
     "return the next random number in the range 0..1"
 
@@ -369,62 +387,6 @@
     "Modified: / 11.11.1999 / 10:08:10 / stefan"
 !
 
-nextIntegerBetween:start and:stop
-    "return an integral random number between start and stop"
-
-    |rnd range bytesNeeded|
-
-    range := stop - start + 1.
-    bytesNeeded := (range highBit + 15) // 8.
-    "Fetch at least 2 bytes, otherwise we get some unbalanced distributions for small ranges"
-    rnd := (LargeInteger digitBytes:(self nextBytes:bytesNeeded)) compressed.
-    rnd := rnd \\ range.
-    ^ rnd + start
-
-    "
-     |r|
-     r := self new.
-     Transcript showCR:(r nextIntegerBetween:1 and:10).
-     Transcript showCR:(r nextIntegerBetween:1 and:10).
-     Transcript showCR:(r nextIntegerBetween:1 and:10).
-     Transcript showCR:(r nextIntegerBetween:1 and:10).
-    "
-
-    "
-     |r bag|
-     r := self new.
-     bag := Bag new.
-     1000000 timesRepeat:[
-         bag add:(r nextIntegerBetween:-1 and:1).
-     ].
-     Transcript showCR:bag sortedCounts.
-    "
-
-    "
-     |r bag|
-     r := self new.
-     bag := Bag new.
-     1000000 timesRepeat:[
-         bag add:(r nextIntegerBetween:1 and:3).
-     ].
-     Transcript showCR:bag sortedCounts.
-     TestCase assert:(bag standardDeviation closeTo:(((3 squared - 1)/12) sqrt)).
-    "
-
-    "
-     |r bag|
-     r := self new.
-     bag := Bag new.
-     1000000 timesRepeat:[
-         bag add:(r nextIntegerBetween:1 and:10).
-     ].
-     Transcript showCR:bag sortedCounts.
-     TestCase assert:(bag standardDeviation closeTo:(((10 squared - 1)/12) sqrt)).
-    "
-
-    "Created: / 11.11.1999 / 10:28:36 / stefan"
-!
-
 nextLettersOrDigits:count
     "get the next count printable letters or digits [0-9A-Za-z]."