1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 11:50:28 +02:00

* 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.
This commit is contained in:
Mikael Djurfeldt 2003-04-06 09:41:07 +00:00
parent 938f6b7c81
commit 372691d8ac
2 changed files with 8 additions and 7 deletions

View file

@ -1,5 +1,9 @@
2003-04-06 Mikael Djurfeldt <djurfeldt@nada.kth.se>
* 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.
* num2integral.i.c (INTEGRAL2BIG): Put negation of n inside then
clause.

View file

@ -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");