#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Fri, 02 Mar 2018 16:15:00 +0100
changeset 4599 169bd0f282fb
parent 4598 409af0c80e09
child 4600 ee760be38331
#FEATURE by cg class: Random added: #nextBytesNonZero:
Random.st
--- a/Random.st	Fri Mar 02 14:15:56 2018 +0100
+++ b/Random.st	Fri Mar 02 16:15:00 2018 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
 ======================================================================
 |
@@ -549,6 +551,33 @@
     "Modified: 1.4.1997 / 22:42:53 / cg"
 !
 
+nextBytesNonZero:count
+    "return count random bytes, none of which is zero (i.e. 1..16rFF each)"
+
+    |res cnt "{Class: SmallInteger}"|
+
+    cnt := count.
+    res := ByteArray uninitializedNew:cnt.
+
+    1 to:cnt do:[:i|
+        |nextNonZero|
+
+        [
+            self step.
+            (nextNonZero := seed bitAnd:16rFF) == 0
+        ] whileTrue.
+        res at:i put:nextNonZero.
+    ].
+
+    ^ res
+
+    "
+     Transcript showCR:(Random new nextBytesNonZero:20).
+    "
+
+    "Modified: 1.4.1997 / 22:42:53 / cg"
+!
+
 nextCharacters:count
     "get the next cnt printable characters.
      We answer printable characters in the ascii range (codepoints 32 - 126)"