zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: PRNG test
@ 2004-12-07 11:55 Peter Stephenson
  0 siblings, 0 replies; only message in thread
From: Peter Stephenson @ 2004-12-07 11:55 UTC (permalink / raw)
  To: Zsh hackers list

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@csr.com>                  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
**********************************************************************


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-12-07 11:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-07 11:55 PATCH: PRNG test Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).