From 0e696e94cdf30c3b5a14a36f2636d1cfe55d5d34 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 17 Dec 2014 21:09:26 -0800 Subject: * rand.c (rand32): Bugfix: if the seed object is a random state, we do not want to make the 8 calls to rand32 to mix up the state; we need are making a straight copy. * txr.1: Document the possibility that the seed object is a random state. Document the platform-independence of the integer seed. --- ChangeLog | 10 ++++++++++ rand.c | 12 +++++++----- txr.1 | 11 ++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3a3eb879..3c798b09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2014-12-17 Kaz Kylheku + + * rand.c (rand32): Bugfix: if the seed object is a random + state, we do not want to make the 8 calls to rand32 to + mix up the state; we need are making a straight copy. + + * txr.1: Document the possibility that the seed object + is a random state. Document the platform-independence of the + integer seed. + 2014-12-17 Kaz Kylheku * rand.c (struct rand_state): Change cur member to unsigned. diff --git a/rand.c b/rand.c index 83d7dcff..5bbad186 100644 --- a/rand.c +++ b/rand.c @@ -110,12 +110,10 @@ static rand32_t rand32(struct rand_state *r) val make_random_state(val seed) { val rs = make_state(); - int i; + int i, copy = 0; struct rand_state *r = coerce(struct rand_state *, cobj_handle(rs, random_state_s)); - r->cur = 0; - seed = default_bool_arg(seed); if (bignump(seed)) { @@ -159,13 +157,17 @@ val make_random_state(val seed) struct rand_state *rseed = coerce(struct rand_state *, cobj_handle(seed, random_state_s)); *r = *rseed; + copy = 1; } else { uw_throwf(error_s, lit("make-random-state: seed ~s is not a number"), seed, nao); } - for (i = 0; i < 8; i++) - (void) rand32(r); + if (!copy) { + r->cur = 0; + for (i = 0; i < 8; i++) + (void) rand32(r); + } return rs; } diff --git a/txr.1 b/txr.1 index 0c3ab384..33cfdfee 100644 --- a/txr.1 +++ b/txr.1 @@ -22992,7 +22992,8 @@ an object of the same kind as what is stored in the .code *random-state* variable. -The seed, if specified, must be an integer value. +The seed, if specified, must be either an integer value, or an +existing random state object. Note that the sign of the seed is ignored, so that negative seed values are equivalent to their additive inverses. @@ -23010,6 +23011,14 @@ On a platform with a millisecond-resolution real-time clock, the minimum time increment is a millisecond. Calls to make-random-state less than a millisecond apart may predictably produce the same seed. +If an integer seed is specified, then the integer value is mapped to a +pseudo-random sequence, in a platform-independent way. + +If a random state is specified as a seed, then it is duplicated. The +returned random state object is a distinct object which is in the same +state as the input object. It will produce the same remaining pseudo-random +number sequence, as will the input object. + .coNP Function @ random-state-p .synb .mets (random-state-p << obj ) -- cgit v1.2.3