c++11: Simplify random generation code using C++11.
* src/misc/random.cc, src/misc/random.hh (srand, drand, mrand, rrand, barand): Simplify using <random> from C++11. (nrand, bmrand, prand): Remove these unused functions. * src/tgbaalgos/randomgraph.cc: Adjust the use of barand. * configure.ac: Do not check for srand48 and drand48.
This commit is contained in:
parent
7480470760
commit
c64503fb33
4 changed files with 18 additions and 147 deletions
|
|
@ -110,7 +110,7 @@ AX_CHECK_BUDDY
|
||||||
AX_CHECK_GSPNLIB
|
AX_CHECK_GSPNLIB
|
||||||
|
|
||||||
AC_CHECK_HEADERS([sys/times.h])
|
AC_CHECK_HEADERS([sys/times.h])
|
||||||
AC_CHECK_FUNCS([times srand48 drand48 kill alarm])
|
AC_CHECK_FUNCS([times kill alarm])
|
||||||
|
|
||||||
LT_CONFIG_LTDL_DIR([ltdl])
|
LT_CONFIG_LTDL_DIR([ltdl])
|
||||||
LT_INIT([win32-dll])
|
LT_INIT([win32-dll])
|
||||||
|
|
|
||||||
|
|
@ -21,121 +21,43 @@
|
||||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "_config.h"
|
|
||||||
#include "random.hh"
|
#include "random.hh"
|
||||||
#include <cstdlib>
|
|
||||||
|
|
||||||
namespace spot
|
namespace spot
|
||||||
{
|
{
|
||||||
|
std::mt19937 gen;
|
||||||
|
|
||||||
void
|
void
|
||||||
srand(unsigned int seed)
|
srand(unsigned int seed)
|
||||||
{
|
{
|
||||||
#if SPOT_HAVE_SRAND48 && SPOT_HAVE_DRAND48
|
gen.seed(seed);
|
||||||
::srand48(seed);
|
|
||||||
#else
|
|
||||||
::srand(seed);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
drand()
|
drand()
|
||||||
{
|
{
|
||||||
#if SPOT_HAVE_SRAND48 && SPOT_HAVE_DRAND48
|
return
|
||||||
return ::drand48();
|
std::generate_canonical<double,
|
||||||
#else
|
std::numeric_limits<double>::digits>(gen);
|
||||||
double r = ::rand();
|
|
||||||
return r / (1.0 + RAND_MAX);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
mrand(int max)
|
mrand(int max)
|
||||||
{
|
{
|
||||||
return static_cast<int>(max * drand());
|
std::uniform_int_distribution<> dis(0, max - 1);
|
||||||
|
return dis(gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
rrand(int min, int max)
|
rrand(int min, int max)
|
||||||
{
|
{
|
||||||
return min + static_cast<int>((max - min + 1) * drand());
|
std::uniform_int_distribution<> dis(min, max);
|
||||||
}
|
return dis(gen);
|
||||||
|
|
||||||
double
|
|
||||||
nrand()
|
|
||||||
{
|
|
||||||
const double r = drand();
|
|
||||||
|
|
||||||
const double lim = 1.e-20;
|
|
||||||
if (r < lim)
|
|
||||||
return -1./lim;
|
|
||||||
if (r > 1.0 - lim)
|
|
||||||
return 1./lim;
|
|
||||||
|
|
||||||
double t;
|
|
||||||
if (r < 0.5)
|
|
||||||
t = sqrt(-2.0 * log(r));
|
|
||||||
else
|
|
||||||
t = sqrt(-2.0 * log(1.0 - r));
|
|
||||||
|
|
||||||
const double p0 = 0.322232431088;
|
|
||||||
const double p1 = 1.0;
|
|
||||||
const double p2 = 0.342242088547;
|
|
||||||
const double p3 = 0.204231210245e-1;
|
|
||||||
const double p4 = 0.453642210148e-4;
|
|
||||||
const double q0 = 0.099348462606;
|
|
||||||
const double q1 = 0.588581570495;
|
|
||||||
const double q2 = 0.531103462366;
|
|
||||||
const double q3 = 0.103537752850;
|
|
||||||
const double q4 = 0.385607006340e-2;
|
|
||||||
const double p = p0 + t * (p1 + t * (p2 + t * (p3 + t * p4)));
|
|
||||||
const double q = q0 + t * (q1 + t * (q2 + t * (q3 + t * q4)));
|
|
||||||
|
|
||||||
if (r < 0.5)
|
|
||||||
return (p / q) - t;
|
|
||||||
else
|
|
||||||
return t - (p / q);
|
|
||||||
}
|
|
||||||
|
|
||||||
double
|
|
||||||
bmrand()
|
|
||||||
{
|
|
||||||
static double next;
|
|
||||||
static bool has_next = false;
|
|
||||||
|
|
||||||
if (has_next)
|
|
||||||
{
|
|
||||||
has_next = false;
|
|
||||||
return next;
|
|
||||||
}
|
|
||||||
|
|
||||||
double x;
|
|
||||||
double y;
|
|
||||||
double r;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
x = 2.0 * drand() - 1.0;
|
|
||||||
y = 2.0 * drand() - 1.0;
|
|
||||||
r = x * x + y * y;
|
|
||||||
}
|
|
||||||
while (r >= 1.0 || r == 0.0);
|
|
||||||
r = sqrt(-2 * log(r) / r);
|
|
||||||
next = y * r;
|
|
||||||
has_next = true;
|
|
||||||
return x * r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
prand(double p)
|
barand::rand()
|
||||||
{
|
{
|
||||||
double s = 0.0;
|
return (*this)(gen);
|
||||||
long x = 0;
|
|
||||||
|
|
||||||
while (s < p)
|
|
||||||
{
|
|
||||||
s -= log(1.0 - drand());
|
|
||||||
++x;
|
|
||||||
}
|
|
||||||
return x - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
# define SPOT_MISC_RANDOM_HH
|
# define SPOT_MISC_RANDOM_HH
|
||||||
|
|
||||||
# include "common.hh"
|
# include "common.hh"
|
||||||
# include <cmath>
|
# include <random>
|
||||||
|
|
||||||
namespace spot
|
namespace spot
|
||||||
{
|
{
|
||||||
|
|
@ -55,69 +55,18 @@ namespace spot
|
||||||
/// \see mrand, rrand, srand
|
/// \see mrand, rrand, srand
|
||||||
SPOT_API double drand();
|
SPOT_API double drand();
|
||||||
|
|
||||||
/// \brief Compute a pseudo-random double value
|
|
||||||
/// following a standard normal distribution. (Odeh & Evans)
|
|
||||||
///
|
|
||||||
/// This uses a polynomial approximation of the inverse cumulated
|
|
||||||
/// density function from Odeh & Evans, Journal of Applied
|
|
||||||
/// Statistics, 1974, vol 23, pp 96-97.
|
|
||||||
SPOT_API double nrand();
|
|
||||||
|
|
||||||
/// \brief Compute a pseudo-random double value
|
|
||||||
/// following a standard normal distribution. (Box-Muller)
|
|
||||||
///
|
|
||||||
/// This uses the polar form of the Box-Muller transform
|
|
||||||
/// to generate random values.
|
|
||||||
SPOT_API double bmrand();
|
|
||||||
|
|
||||||
/// \brief Compute pseudo-random integer value between 0
|
/// \brief Compute pseudo-random integer value between 0
|
||||||
/// and \a n included, following a binomial distribution
|
/// and \a n included, following a binomial distribution
|
||||||
/// for probability \a p.
|
/// for probability \a p.
|
||||||
///
|
class SPOT_API barand : protected std::binomial_distribution<>
|
||||||
/// \a gen must be a random function computing a pseudo-random
|
|
||||||
/// double value following a standard normal distribution.
|
|
||||||
/// Use nrand() or bmrand().
|
|
||||||
///
|
|
||||||
/// Usually approximating a binomial distribution using a normal
|
|
||||||
/// distribution and is accurate only if <code>n*p</code> and
|
|
||||||
/// <code>n*(1-p)</code> are greater than 5.
|
|
||||||
template<double (*gen)()>
|
|
||||||
class barand
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
barand(int n, double p)
|
barand(int n, double p) : binomial_distribution(n, p)
|
||||||
: n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int rand();
|
||||||
rand() const
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
double x = gen() * s_ + m_;
|
|
||||||
if (x < 0.0)
|
|
||||||
continue;
|
|
||||||
res = static_cast<int> (x);
|
|
||||||
if (res <= n_)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
protected:
|
|
||||||
const int n_;
|
|
||||||
const double m_;
|
|
||||||
const double s_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// \brief Return a pseudo-random positive integer value
|
|
||||||
/// following a Poisson distribution with parameter \a p.
|
|
||||||
///
|
|
||||||
/// \pre <code>p > 0</code>
|
|
||||||
SPOT_API int prand(double p);
|
|
||||||
/// @}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // SPOT_MISC_RANDOM_HH
|
#endif // SPOT_MISC_RANDOM_HH
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,7 @@ namespace spot
|
||||||
// We want to connect each node to a number of successors between
|
// We want to connect each node to a number of successors between
|
||||||
// 1 and n. If the probability to connect to each successor is d,
|
// 1 and n. If the probability to connect to each successor is d,
|
||||||
// the number of connected successors follows a binomial distribution.
|
// the number of connected successors follows a binomial distribution.
|
||||||
barand<nrand> bin(n - 1, d);
|
barand bin(n - 1, d);
|
||||||
|
|
||||||
while (!nodes_to_process.empty())
|
while (!nodes_to_process.empty())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue