random: fix rounding in barand()

This fixes #85.

* src/misc/random.hh (barand): Use round() before casting.
* doc/org/oaut.org: Recompute example.
* src/tests/randaut.test, wrap/python/tests/randaut.ipynb: Adjust.
This commit is contained in:
Alexandre Duret-Lutz 2015-06-01 19:43:21 +02:00
parent eabed370bf
commit a75a9c091c
4 changed files with 2073 additions and 1800 deletions

View file

@ -23,6 +23,7 @@
#pragma once
#include "common.hh"
#include <cassert>
#include <cmath>
#include <vector>
@ -72,7 +73,7 @@ namespace spot
/// \brief Compute pseudo-random integer value between 0
/// and \a n included, following a binomial distribution
/// for probability \a p.
/// with probability \a p.
///
/// \a gen must be a random function computing a pseudo-random
/// double value following a standard normal distribution.
@ -93,18 +94,16 @@ namespace spot
int
rand() const
{
int res;
for (;;)
{
double x = gen() * s_ + m_;
if (x < 0.0)
int x = round(gen() * s_ + m_);
if (x < 0)
continue;
res = static_cast<int> (x);
if (res <= n_)
break;
if (x <= n_)
return x;
}
return res;
SPOT_UNREACHABLE();
return 0;
}
protected:
const int n_;