diff options
author | Linus Nordberg <linus@nordberg.se> | 1999-03-16 11:30:46 +0100 |
---|---|---|
committer | Linus Nordberg <linus@nordberg.se> | 1999-03-16 11:30:46 +0100 |
commit | 6b271ada539875ce6a09a4f50a64bd9785a2a6de (patch) | |
tree | 266be964a407bcbce55bb8a0087e959e4c7ac485 /mpf | |
parent | 9d88b9a05385660699543e5c64b7562ee36d6034 (diff) | |
download | gmp-6b271ada539875ce6a09a4f50a64bd9785a2a6de.tar.gz |
New randomization functions.
Diffstat (limited to 'mpf')
-rw-r--r-- | mpf/Makefile.in | 5 | ||||
-rw-r--r-- | mpf/urandom.c | 61 |
2 files changed, 64 insertions, 2 deletions
diff --git a/mpf/Makefile.in b/mpf/Makefile.in index ae9a580a6..8c0c385bf 100644 --- a/mpf/Makefile.in +++ b/mpf/Makefile.in @@ -36,7 +36,7 @@ MPF_SRCS = init.c init2.c set.c set_ui.c set_si.c set_str.c set_d.c set_z.c \ add.c add_ui.c sub.c sub_ui.c ui_sub.c mul.c mul_ui.c div.c div_ui.c \ cmp.c cmp_ui.c cmp_si.c mul_2exp.c div_2exp.c abs.c neg.c set_q.c get_d.c \ set_dfl_prec.c set_prc.c set_prc_raw.c get_prc.c ui_div.c sqrt_ui.c \ - integer.c pow_ui.c + integer.c pow_ui.c urandom.c MPF_OBJS = init.o init2.o \ set.o set_ui.o set_si.o set_str.o set_d.o set_z.o set_q.o \ iset.o iset_ui.o iset_si.o iset_str.o iset_d.o clear.o get_str.o \ @@ -44,7 +44,7 @@ MPF_OBJS = init.o init2.o \ add.o add_ui.o sub.o sub_ui.o ui_sub.o mul.o mul_ui.o div.o div_ui.o \ cmp.o cmp_ui.o cmp_si.o mul_2exp.o div_2exp.o abs.o neg.o get_d.o \ set_dfl_prec.o set_prc.o set_prc_raw.o get_prc.o ui_div.o sqrt_ui.o \ - floor.o ceil.o trunc.o pow_ui.o + floor.o ceil.o trunc.o pow_ui.o urandom.o LATER_OBJS = inp_raw.o out_raw.o random.o fac_ui.o @@ -129,3 +129,4 @@ sub.o: $(srcdir)/sub.c $(H) sub_ui.o: $(srcdir)/sub_ui.c $(H) ui_div.o: $(srcdir)/ui_div.c $(H) $(srcdir)/../longlong.h ui_sub.o: $(srcdir)/ui_sub.c $(H) +urandom.o: $(srcdir)/urandom.c $(H) diff --git a/mpf/urandom.c b/mpf/urandom.c new file mode 100644 index 000000000..6fb929a5d --- /dev/null +++ b/mpf/urandom.c @@ -0,0 +1,61 @@ +/* urandomb(rop, state) -- Generate a uniform pseudorandom real number +between zero and one, using `state' as the random state previously +initialized by a call to gmp_rand_init(). + +Copyright (C) 1999 Free Software Foundation, Inc. + +This file is part of the GNU MP Library. + +The GNU MP Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Library General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at your +option) any later version. + +The GNU MP Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public +License for more details. + +You should have received a copy of the GNU Library General Public License +along with the GNU MP Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, +MA 02111-1307, USA. */ + +#include "gmp.h" + +/* mpf_urandomb() -- Generate a universally distributed real random +number 0 <= X < 1. See file mpz/urandom.c for algorithms used. */ + +void +#if __STDC__ +mpf_urandomb (mpf_t rop, gmp_rand_state *s) +#else +mpf_urandomb (rop, s) + mpf_t rop; + gmp_rand_state *s; +#endif +{ + mpz_t z_rand; + mpf_t f_tmp; + + switch (s->alg) + { + case GMP_RAND_ALG_LC: + mpz_init (z_rand); + mpf_init (f_tmp); + + /* FIXME: mpz_urandomb() will most often do a division and a + multiplication in order to produce 0 <= z < 2^size-1. We + only need the division. Break up mpz_urandomb(). */ + + mpz_urandomb (z_rand, s); + mpf_set_z (rop, z_rand); + mpf_set_z (f_tmp, s->scheme->m); + mpf_div (rop, rop, f_tmp); + + mpz_clear (z_rand); + mpf_clear (f_tmp); + + break; + } +} |