Fixed a bug where the random number generator always returned zero.
authorJohn Darrington <john@darrington.wattle.id.au>
Tue, 3 Feb 2004 07:03:08 +0000 (07:03 +0000)
committerJohn Darrington <john@darrington.wattle.id.au>
Tue, 3 Feb 2004 07:03:08 +0000 (07:03 +0000)
po/en_GB.po
src/glob.c
src/random.c
src/set.q
tests/Makefile.am
tests/bugs/random.sh [new file with mode: 0755]

index 5856dfb363711d00962f3a52a55d05ad7893ea89..cb151b871b674dab0f6e415d7ef03244d89d77c1 100644 (file)
@@ -6,6 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PSPP 0.3.1\n"
+"Report-Msgid-Bugs-To: pspp-dev@gnu.org\n"
 "POT-Creation-Date: 2004-02-03 09:11+0800\n"
 "PO-Revision-Date: 2004-01-23 13:04+0800\n"
 "Last-Translator: John Darrington <john@darrington.wattle.id.au>\n"
@@ -13,7 +14,6 @@ msgstr ""
 "MIME-Version: 1.0\n"
 "Content-Type: text/plain; charset=iso-8859-1\n"
 "Content-Transfer-Encoding: 8bit\n"
-"Report-Msgid-Bugs-To: pspp-dev@gnu.org\n"
 "Plural-Forms: nplurals=2; plural=(n!=1);\n"
 
 #: src/crosstabs.q:255
index 958edc5f221f34ab36e3a8b7a64a7b2f5e985db6..ca71e9e8063265a7842825bb2842eb5095d4e265 100644 (file)
@@ -258,7 +258,7 @@ init_glob (int argc unused, char **argv)
   set_nullline = 1;
   set_more = 1;
   set_prompt = xstrdup ("PSPP> ");
-  set_seed = 2000000;
+  set_seed = NOT_LONG;
 
 #if __DJGPP__ || __BORLANDC__
   {
index ab78b32bb256f19c121c2de34a43529cdb9684d9..163986cc959e319df0b32a26c16fe78b2f6a44b8 100644 (file)
@@ -46,11 +46,16 @@ struct rng *
 rng_create (void) 
 {
   struct rng *rng;
-  static time_t t;
+  static time_t t=0;
 
   rng = xmalloc (sizeof *rng);
-  if (t == 0)
-    time (&t);
+  if (t == 0 || set_seed == NOT_LONG)
+  {
+    if (set_seed == NOT_LONG) 
+      time (&t);
+    else
+      t = set_seed;
+  }
   else
     t++;
   rng_seed (rng, &t, sizeof t);
@@ -157,7 +162,7 @@ rng_get_double (struct rng *rng)
   unsigned long value;
 
   rng_get_bytes (rng, &value, sizeof value);
-  return value / ULONG_MAX;
+  return value / (double) ULONG_MAX;
 }
 
 /* Returns a random number from the distribution with mean 0 and
index 439d52a2ec2a3c12f76add3d1f5b722321bba1bc..c4f80326b905f95507e34a65eb19cd8f961a03f1 100644 (file)
--- a/src/set.q
+++ b/src/set.q
@@ -114,7 +114,6 @@ int set_safer;
 int set_scompression;
 int set_screen;
 long set_seed;
-int set_seed_used;
 int set_testing_mode;
 int set_undefined;
 int set_viewlength;
index b1746e9707e6a7cfa78a44f7abe04000b08d6ec2..fe8de35d2cf6b425c8265146a0759a8b359c9077 100644 (file)
@@ -25,7 +25,8 @@ TESTS = command/aggregate.sh \
        bugs/alpha-freq.sh \
        bugs/double-frequency.sh \
        bugs/html-frequency.sh \
-       bugs/crosstabs.sh
+       bugs/crosstabs.sh \
+       bugs/random.sh
 
 noinst_PROGRAMS = gengarbage
 
diff --git a/tests/bugs/random.sh b/tests/bugs/random.sh
new file mode 100755 (executable)
index 0000000..cb7fb06
--- /dev/null
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+# This program tests for a bug which caused UNIFORM(x) to always return zero.
+
+
+TEMPDIR=/tmp/pspp-tst-$$
+
+here=`pwd`;
+
+# ensure that top_srcdir is absolute
+cd $top_srcdir; top_srcdir=`pwd`
+
+export STAT_CONFIG_PATH=$top_srcdir/config
+
+
+cleanup()
+{
+     rm -rf $TEMPDIR
+}
+
+
+fail()
+{
+    echo $activity
+    echo FAILED
+    cleanup;
+    exit 1;
+}
+
+
+no_result()
+{
+    echo $activity
+    echo NO RESULT;
+    cleanup;
+    exit 2;
+}
+
+pass()
+{
+    cleanup;
+    exit 0;
+}
+
+mkdir -p $TEMPDIR
+
+cd $TEMPDIR
+
+activity="create program"
+cat > $TEMPDIR/rnd.sps <<EOF
+set seed=10.
+input program.
++ loop #i = 1 to 20.
++    do repeat response=r1.
++       compute response = uniform(10).
++    end repeat.
++    end case.
++ end loop.
++ end file.
+end input program.                                                              
+
+list.
+EOF
+if [ $? -ne 0 ] ; then no_result ; fi
+
+$here/../src/pspp -o raw-ascii $TEMPDIR/rnd.sps
+if [ $? -ne 0 ] ; then no_result ; fi
+
+
+diff -b -B -w $TEMPDIR/pspp.list - << EOF
+      R1
+--------
+    2.36 
+    3.13 
+    1.76 
+     .15 
+    5.88 
+    8.74 
+    2.19 
+    6.53 
+    5.69 
+    6.77 
+    7.20 
+    4.01 
+     .03 
+    4.67 
+    5.10 
+     .44 
+    8.27 
+    6.81 
+    9.55 
+    8.74 
+EOF
+if [ $? -ne 0 ] ; then fail ; fi
+
+pass;