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:
parent
938f6b7c81
commit
372691d8ac
2 changed files with 8 additions and 7 deletions
|
@ -1,5 +1,9 @@
|
||||||
2003-04-06 Mikael Djurfeldt <djurfeldt@nada.kth.se>
|
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
|
* num2integral.i.c (INTEGRAL2BIG): Put negation of n inside then
|
||||||
clause.
|
clause.
|
||||||
|
|
||||||
|
|
|
@ -248,7 +248,6 @@ SCM
|
||||||
scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
scm_c_random_bignum (scm_t_rstate *state, SCM m)
|
||||||
{
|
{
|
||||||
SCM result = scm_i_mkbig ();
|
SCM result = scm_i_mkbig ();
|
||||||
int finished = 0;
|
|
||||||
const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
|
const size_t m_bits = mpz_sizeinbase (SCM_I_BIG_MPZ (m), 2);
|
||||||
/* how many bits would only partially fill the last unsigned long? */
|
/* how many bits would only partially fill the last unsigned long? */
|
||||||
const size_t end_bits = m_bits % (sizeof (unsigned long) * 8);
|
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
|
/* FIXME: what about chance that bignums end up with zeroes at the
|
||||||
front? -- do we need to normalize? */
|
front? -- do we need to normalize? */
|
||||||
|
|
||||||
while (!finished)
|
do
|
||||||
{
|
{
|
||||||
unsigned long *current_chunk = random_chunks + (num_chunks - 1);
|
unsigned long *current_chunk = random_chunks + (num_chunks - 1);
|
||||||
unsigned long chunks_left = num_chunks;
|
unsigned long chunks_left = num_chunks;
|
||||||
int cmp;
|
|
||||||
|
|
||||||
mpz_set_ui (SCM_I_BIG_MPZ (result), 0);
|
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,
|
||||||
0,
|
0,
|
||||||
random_chunks);
|
random_chunks);
|
||||||
cmp = mpz_cmp (SCM_I_BIG_MPZ (m), SCM_I_BIG_MPZ (result));
|
/* if result >= m, regenerate it (it is important to regenerate
|
||||||
if (cmp >= 0)
|
all bits in order not to get a distorted distribution) */
|
||||||
finished = 1;
|
} while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= 0);
|
||||||
}
|
|
||||||
scm_gc_free (random_chunks,
|
scm_gc_free (random_chunks,
|
||||||
num_chunks * sizeof (unsigned long),
|
num_chunks * sizeof (unsigned long),
|
||||||
"random bignum chunks");
|
"random bignum chunks");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue