[BACK]Return to urandomm.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/urandomm.c, Revision 1.1.1.2

1.1       maekawa     1: /* mpz_urandomm (rop, state, n) -- Generate a uniform pseudorandom
                      2:    integer in the range 0 to N-1, using STATE as the random state
                      3:    previously initialized by a call to gmp_randinit().
                      4:
                      5: Copyright (C) 2000  Free Software Foundation, Inc.
                      6:
                      7: This file is part of the GNU MP Library.
                      8:
                      9: The GNU MP Library is free software; you can redistribute it and/or modify
                     10: it under the terms of the GNU Lesser General Public License as published by
                     11: the Free Software Foundation; either version 2.1 of the License, or (at your
                     12: option) any later version.
                     13:
                     14: The GNU MP Library is distributed in the hope that it will be useful, but
                     15: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     16: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     17: License for more details.
                     18:
                     19: You should have received a copy of the GNU Lesser General Public License
                     20: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     21: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     22: MA 02111-1307, USA. */
                     23:
                     24: #include "gmp.h"
                     25: #include "gmp-impl.h"
                     26: #include "longlong.h"
                     27:
                     28: void
                     29: #if __STDC__
                     30: mpz_urandomm (mpz_t rop, gmp_randstate_t rstate, mpz_t n)
                     31: #else
                     32: mpz_urandomm (rop, rstate, n)
                     33:      mpz_t rop;
                     34:      gmp_randstate_t rstate;
                     35:      mpz_t n;
                     36: #endif
                     37: {
                     38:   mpz_t t, p, m;
                     39:   mp_ptr tp;
                     40:   mp_size_t nbits, size;
                     41:   int count;
1.1.1.2 ! maekawa    42:   TMP_DECL (marker);
        !            43:
        !            44:   TMP_MARK (marker);
1.1       maekawa    45:
                     46:   /* FIXME: Should check for n == 0 and report error */
                     47:
                     48:   size = SIZ (n);
                     49:   count_leading_zeros (count, PTR (n)[size - 1]);
                     50:   nbits = size * BITS_PER_MP_LIMB - count;
                     51:
                     52:   /* Allocate enough for any mpz function called since a realloc of
                     53:      these will fail.  */
                     54:   MPZ_TMP_INIT (t, size);
                     55:   MPZ_TMP_INIT (m, size + 1);
                     56:   MPZ_TMP_INIT (p, size + 1);
                     57:
                     58:   /* Let m = highest possible random number plus 1.  */
                     59:   mpz_set_ui (m, 0);
                     60:   mpz_setbit (m, nbits);
                     61:
                     62:   /* Let p = floor(m / n) * n.  */
                     63:   mpz_fdiv_q (p, m, n);
                     64:   mpz_mul (p, p, n);
                     65:
                     66:   tp = PTR (t);
                     67:   do
                     68:     {
                     69:       _gmp_rand (tp, rstate, nbits);
                     70:       MPN_NORMALIZE (tp, size);        /* FIXME: Really necessary?  */
                     71:       SIZ (t) = size;
                     72:     }
                     73:   while (mpz_cmp (t, p) >= 0);
                     74:
                     75:   mpz_mod (rop, t, n);
1.1.1.2 ! maekawa    76:
        !            77:   TMP_FREE (marker);
1.1       maekawa    78: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>