Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: PRNG test



Here's a test that the two pseudorandom number generators, $RANDOM and
rand48(),  are probably wired up correctly.  There's a more detailed
test of the logic for rand48() elsewhere.

This is not designed to test the distribution in a mathematically
sophisticated way, just that the function is producing something which
is more likely to be pseudorandom than, say, the temperature or the
number of bugs in the firmware.

(There are no bugs in our firmware, of course.)

If there are no comments about the parameter patch (e.g. "what the hell
is this?") I will check it in this afternoon GMT.

Index: Test/V03mathfunc.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/V03mathfunc.ztst,v
retrieving revision 1.1
diff -u -r1.1 V03mathfunc.ztst
--- Test/V03mathfunc.ztst	9 Jun 2004 12:06:19 -0000	1.1
+++ Test/V03mathfunc.ztst	7 Dec 2004 11:47:53 -0000
@@ -104,3 +104,35 @@
    print $(( sqrt(-1) ))
 1:Non-negative argument checking for square roots.
 ?(eval):1: math: argument to sqrt out of range
+
+# Simple test that the pseudorandom number generators are producing
+# something that could conceivably be pseudorandom numbers in a
+# linear range.  Not a detailed quantitative verification.
+  integer N=10000 isource ok=1
+  float -F f sum sumsq max max2 av sd
+  typeset -a randoms
+  randoms=('f = RANDOM' 'f = rand48()')
+  zmodload -i zsh/mathfunc
+  for isource in 1 2; do
+    (( sum = sumsq = max = 0 ))
+    repeat $N; do
+      let $randoms[$isource]
+      (( f > max )) && (( max = f ))
+      (( sum += f, sumsq += f * f ))
+    done
+    (( av = sum / N ))
+    (( sd = sqrt((sumsq - N * av * av) / (N-1)) ))
+    (( max2 = 0.5 * max ))
+    if (( av > max2 * 1.1 )) || (( av < max2 * 0.9 )); then
+      print "WARNING: average of random numbers is suspicious.
+  Was testing: $randoms[$isource]"
+      (( ok = 0 ))
+    fi
+    if (( sd < max / 4 )); then
+      print "WARNING: distribution of random numbers is suspicious.
+  Was testing: $randoms[$isource]"
+      (( ok = 0 ))
+    fi
+  done
+  (( ok ))
+0:Test random number generator distributions are not grossly broken

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



Messages sorted by: Reverse Date, Date, Thread, Author