From: John Darrington Date: Tue, 3 Feb 2004 07:03:08 +0000 (+0000) Subject: Fixed a bug where the random number generator always returned zero. X-Git-Tag: v0.4.0~383 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f61e279fa8d35fa2f9c5ef6d5d0e628147ccde60;p=pspp-builds.git Fixed a bug where the random number generator always returned zero. --- diff --git a/po/en_GB.po b/po/en_GB.po index 5856dfb3..cb151b87 100644 --- a/po/en_GB.po +++ b/po/en_GB.po @@ -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 \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 diff --git a/src/glob.c b/src/glob.c index 958edc5f..ca71e9e8 100644 --- a/src/glob.c +++ b/src/glob.c @@ -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__ { diff --git a/src/random.c b/src/random.c index ab78b32b..163986cc 100644 --- a/src/random.c +++ b/src/random.c @@ -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 diff --git a/src/set.q b/src/set.q index 439d52a2..c4f80326 100644 --- 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; diff --git a/tests/Makefile.am b/tests/Makefile.am index b1746e97..fe8de35d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 00000000..cb7fb06a --- /dev/null +++ b/tests/bugs/random.sh @@ -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 <