12 #if !defined(RANDOMLIB_MPFRNORMAL_HPP)
13 #define RANDOMLIB_MPFRNORMAL_HPP 1
18 #if HAVE_MPFR || defined(DOXYGEN)
52 { Compute(r);
return _x.swap(t); }
62 int operator()(mpfr_t val, gmp_randstate_t r, mpfr_rnd_t round)
const
63 { Compute(r);
return _x(val, r, round); }
69 int ExpProbH(gmp_randstate_t r)
const {
70 _p.Init();
if (_p.TestHighBit(r))
return 1;
73 _q.Init();
if (!_q.LessThan(r, _p))
return 0;
74 _p.Init();
if (!_p.LessThan(r, _q))
return 1;
78 int ExpProb(gmp_randstate_t r,
unsigned n)
const {
79 while (n--) {
if (!ExpProbH(r))
return 0; }
83 unsigned ExpProbN(gmp_randstate_t r)
const {
85 while (ExpProbH(r)) ++n;
92 int Choose(gmp_randstate_t r,
int k)
const {
94 const int m = 2 * k + 2;
95 int n1 = m - 2, n2 = m - 1;
97 mpz_urandomb(_tt, r, b);
98 int d = int( mpz_get_ui(_tt) ) * m;
99 n1 = (std::max)((n1 << b) - d, 0);
100 if (n1 >= m)
return 1;
101 n2 = (std::min)((n2 << b) - d, m);
102 if (n2 <= 0)
return -1;
103 if (n1 == 0 && n2 == m)
return 0;
106 void Compute(gmp_randstate_t r)
const {
108 unsigned k = ExpProbN(r);
109 if (ExpProb(r, (k - 1) * k)) {
112 for (
unsigned j = 0; j <= k; ++j) {
114 for (s = 1, first =
true; ; s ^= 1, first =
false) {
115 if (k == 0 && _x.Boolean(r))
break;
116 _q.Init();
if (!_q.LessThan(r, first ? _x : _p))
break;
117 int y = k == 0 ? 0 : Choose(r, k);
121 _p.Init();
if (!_p.LessThan(r, _x))
break;
129 if (_x.Boolean(r)) _x.Negate();
136 mutable MPFRRandom<bits> _x;
137 mutable MPFRRandom<bits> _p;
138 mutable MPFRRandom<bits> _q;
144 #endif // RANDOMLIB_MPFRNORMAL_HPP
int operator()(mpfr_t val, gmp_randstate_t r, mpfr_rnd_t round) const
void operator()(MPFRRandom< bits > &t, gmp_randstate_t r) const
Handling random numbers in MPFR.
The normal distribution for MPFR.