Fix urandom stuff

This commit is contained in:
Albin Ahlbäck 2021-05-18 22:52:35 +02:00
parent 1f62c81be8
commit f4fe05b8a2
5 changed files with 30 additions and 12 deletions

2
arb.h
View file

@ -333,7 +333,7 @@ void arb_randtest(arb_t x, flint_rand_t state, slong prec, slong mag_bits);
void arb_randtest_special(arb_t x, flint_rand_t state, slong prec, slong mag_bits);
void arb_urandom(arb_t x, flint_rand_t state, slong prec, arf_rnd_t rnd);
void arb_urandom(arb_t x, flint_rand_t state, slong prec);
void arb_add_error_arf(arb_t x, const arf_t err);

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2021 Fredrik Johansson
Copyright (C) 2021 Albin Ahlbäck
This file is part of Arb.
@ -18,7 +18,6 @@ int main()
slong iter;
slong prec;
flint_rand_t state;
arf_rnd_t round;
arb_t rand[N];
arb_t m; /* mean */
arb_t s; /* variance */
@ -34,12 +33,11 @@ int main()
arb_init(mp);
arb_init(sp);
prec = 299;
round = ARF_RND_DOWN;
for (iter = 0; iter < N; iter++)
{
arb_init(rand[iter]);
arb_urandom(rand[iter], state, prec, round);
arb_urandom(rand[iter], state, prec);
arb_add(m, m, rand[iter], prec);
}
@ -77,6 +75,8 @@ int main()
for (iter = 0; iter < N; iter++) arb_clear(rand[iter]);
arb_clear(m);
arb_clear(s);
arb_clear(mp);
arb_clear(sp);
flint_randclear(state);
flint_cleanup();
flint_printf("PASS\n");

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2021 Fredrik Johansson
Copyright (C) 2021 Albin Ahlbäck
This file is part of Arb.
@ -12,9 +12,25 @@
#include "arb.h"
void
arb_urandom(arb_t x, flint_rand_t state, slong prec, arf_rnd_t rnd)
arb_urandom(arb_t x, flint_rand_t state, slong bits)
{
arf_urandom(arb_midref(x), state, prec, rnd);
mag_zero(arb_radref(x));
slong prec = bits;
fmpz_t n;
fmpz_t t;
prec += 128;
fmpz_init(n);
fmpz_one(n);
fmpz_mul_2exp(n, n, (ulong) prec);
fmpz_init(t);
fmpz_randm(t, state, n);
arb_set_round_fmpz(x, t, bits);
arb_mul_2exp_si(x, x, -prec);
fmpz_clear(n);
fmpz_clear(t);
}

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2021 Fredrik Johansson
Copyright (C) 2021 Albin Ahlbäck
This file is part of Arb.
@ -30,5 +30,7 @@ arf_urandom(arf_t x, flint_rand_t state, slong bits, arf_rnd_t rnd)
arf_set_round_fmpz(x, t, bits, rnd);
arf_mul_2exp_si(x, x, -prec);
fmpz_clear(n);
fmpz_clear(t);
}

View file

@ -373,8 +373,8 @@ Random number generation
.. function:: void arb_urandom(arb_t x, flint_rand_t state, slong prec, arf_rnd_t rnd)
Sets *x* to a uniformly distributed random number in the interval
`[0, 1]`. The method uses rounding from integers to floats based on the
rounding mode *rnd*.
`[0, 1]`. The method uses rounding from integers to floats, hence the
radius might not be `0`.
Radius and interval operations
-------------------------------------------------------------------------------