* src/misc/random.hh (nrand, bmrand, prand): New functions.
(barand): New class. * src/misc/random.cc (nrand, bmrand, prand): New functions. * wrap/python/spot.i: Process src/misc/random.hh.
This commit is contained in:
parent
d771a3a019
commit
541705a36a
4 changed files with 150 additions and 1 deletions
|
|
@ -57,4 +57,82 @@ namespace spot
|
|||
return min + static_cast<int>((max - min + 1) * drand());
|
||||
}
|
||||
|
||||
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
|
||||
prand(double p)
|
||||
{
|
||||
double s = 0.0;
|
||||
long x = 0;
|
||||
|
||||
while (s < p)
|
||||
{
|
||||
s -= log(1.0 - drand());
|
||||
++x;
|
||||
}
|
||||
return x - 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue