186d7f5d3SJohn Marino /* mpf_urandomb (rop, state, nbits) -- Generate a uniform pseudorandom
286d7f5d3SJohn Marino real number between 0 (inclusive) and 1 (exclusive) of size NBITS,
386d7f5d3SJohn Marino using STATE as the random state previously initialized by a call to
486d7f5d3SJohn Marino gmp_randinit().
586d7f5d3SJohn Marino
686d7f5d3SJohn Marino Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
786d7f5d3SJohn Marino
886d7f5d3SJohn Marino This file is part of the GNU MP Library.
986d7f5d3SJohn Marino
1086d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1186d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1286d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1386d7f5d3SJohn Marino option) any later version.
1486d7f5d3SJohn Marino
1586d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1686d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1786d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1886d7f5d3SJohn Marino License for more details.
1986d7f5d3SJohn Marino
2086d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2186d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
2286d7f5d3SJohn Marino
2386d7f5d3SJohn Marino #include "gmp.h"
2486d7f5d3SJohn Marino #include "gmp-impl.h"
2586d7f5d3SJohn Marino
2686d7f5d3SJohn Marino void
mpf_urandomb(mpf_t rop,gmp_randstate_t rstate,mp_bitcnt_t nbits)2786d7f5d3SJohn Marino mpf_urandomb (mpf_t rop, gmp_randstate_t rstate, mp_bitcnt_t nbits)
2886d7f5d3SJohn Marino {
2986d7f5d3SJohn Marino mp_ptr rp;
3086d7f5d3SJohn Marino mp_size_t nlimbs;
3186d7f5d3SJohn Marino mp_exp_t exp;
3286d7f5d3SJohn Marino mp_size_t prec;
3386d7f5d3SJohn Marino
3486d7f5d3SJohn Marino rp = PTR (rop);
3586d7f5d3SJohn Marino nlimbs = BITS_TO_LIMBS (nbits);
3686d7f5d3SJohn Marino prec = PREC (rop);
3786d7f5d3SJohn Marino
3886d7f5d3SJohn Marino if (nlimbs > prec + 1 || nlimbs == 0)
3986d7f5d3SJohn Marino {
4086d7f5d3SJohn Marino nlimbs = prec + 1;
4186d7f5d3SJohn Marino nbits = nlimbs * GMP_NUMB_BITS;
4286d7f5d3SJohn Marino }
4386d7f5d3SJohn Marino
4486d7f5d3SJohn Marino _gmp_rand (rp, rstate, nbits);
4586d7f5d3SJohn Marino
4686d7f5d3SJohn Marino /* If nbits isn't a multiple of GMP_NUMB_BITS, shift up. */
4786d7f5d3SJohn Marino if (nbits % GMP_NUMB_BITS != 0)
4886d7f5d3SJohn Marino mpn_lshift (rp, rp, nlimbs, GMP_NUMB_BITS - nbits % GMP_NUMB_BITS);
4986d7f5d3SJohn Marino
5086d7f5d3SJohn Marino exp = 0;
5186d7f5d3SJohn Marino while (nlimbs != 0 && rp[nlimbs - 1] == 0)
5286d7f5d3SJohn Marino {
5386d7f5d3SJohn Marino nlimbs--;
5486d7f5d3SJohn Marino exp--;
5586d7f5d3SJohn Marino }
5686d7f5d3SJohn Marino EXP (rop) = exp;
5786d7f5d3SJohn Marino SIZ (rop) = nlimbs;
5886d7f5d3SJohn Marino }
59