diff options
author | Mikael Djurfeldt <djurfeldt@nada.kth.se> | 2003-04-06 09:41:07 +0000 |
---|---|---|
committer | Mikael Djurfeldt <djurfeldt@nada.kth.se> | 2003-04-06 09:41:07 +0000 |
commit | 372691d8ac81acebeb031f853ac6d30d83c20ed6 (patch) | |
tree | d9935c8f6f5404949e390a63dc1b7de793942484 /libguile/random.c | |
parent | 938f6b7c811b0b245d8d85062f68eddd11b4f170 (diff) | |
download | guile-372691d8ac81acebeb031f853ac6d30d83c20ed6.tar.gz |
* random.c (scm_c_random_bignum): Don't generate a random number
equal to m (the second argument of scm_c_random_bignum); only
generate numbers in the range 0 <= r < m.
Diffstat (limited to 'libguile/random.c')
-rw-r--r-- | libguile/random.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/libguile/random.c b/libguile/random.c index e41b9eb53..bbea3b41d 100644 --- a/libguile/random.c +++ b/libguile/random.c @@ -248,7 +248,6 @@ SCM scm_c_random_bignum (scm_t_rstate *state, SCM m) { SCM result = scm_i_mkbig (); - int finished = 0; const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2); /* how many bits would only partially fill the last unsigned long? */ const size_t end_bits = m_bits % (sizeof (unsigned long) * 8); @@ -266,11 +265,10 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m) /* FIXME: what about chance that bignums end up with zeroes at the front? -- do we need to normalize? */ - while (!finished) + do { unsigned long *current_chunk = random_chunks + (num_chunks - 1); unsigned long chunks_left = num_chunks; - int cmp; mpz_set_ui (SCM_I_BIG_MPZ (result), 0); @@ -299,10 +297,9 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m) 0, 0, random_chunks); - cmp = mpz_cmp (SCM_I_BIG_MPZ (m), SCM_I_BIG_MPZ (result)); - if (cmp >= 0) - finished = 1; - } + /* if result >= m, regenerate it (it is important to regenerate + all bits in order not to get a distorted distribution) */ + } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= 0); scm_gc_free (random_chunks, num_chunks * sizeof (unsigned long), "random bignum chunks"); |