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

@ -866,29 +866,27 @@ In most tools =%F= and =%L= are the input filename and line number,
but as this makes no sense in =randaut=, these two sequences emit
numbers related to the generation of automata.
For instance let's generate 100 random automata with 10 states and
For instance let's generate 1000 random automata with 100 states and
density 0.2, and just count the number of edges in each automaton. Then
use =R= to summarize the distribution of these values:
#+BEGIN_SRC sh :results verbatim :exports both
randaut -d0.2 -Q10 -n1000 a --stats %e > size.csv
randaut -d0.2 -Q100 -n1000 a --stats %e > size.csv
R --slave -e "summary(read.csv('size.csv', header=FALSE, col.names='edges'))"
#+END_SRC
#+RESULTS:
: edges
: Min. :14.00
: 1st Qu.:22.00
: Median :25.00
: Mean :24.72
: 3rd Qu.:27.00
: Max. :36.00
For $Q=10$ states and density $D=0.2$ the expected degree of each
state is $1+(Q-1)D = 1+9\times 0.2 = 2.8$, so the expected number of
edges should be 10 times that.
: Min. :1939
: 1st Qu.:2056
: Median :2083
: Mean :2082
: 3rd Qu.:2107
: Max. :2233
For $Q=100$ states and density $D=0.2$ the expected degree of each
state is $1+(Q-1)D = 1+99\times 0.2 = 20.8$, so the expected number of
edges should be $20.8\times100=2080$.
* Naming automata

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_;

View file

@ -59,10 +59,10 @@ test `expr $a + $b` = 100
$randaut -n 5 --name='%F-%L-%s-%c-%e' -H a | grep '^name' >out
cat >expected<<EOF
name: "0-0-10-1-30"
name: "0-1-10-4-27"
name: "0-2-10-6-20"
name: "0-3-10-1-25"
name: "0-4-10-2-20"
name: "0-1-10-1-29"
name: "0-2-10-3-22"
name: "0-3-10-1-30"
name: "0-4-10-1-27"
EOF
diff out expected
@ -97,19 +97,19 @@ $a: 4 Inf(0)&Inf(1)&Inf(2)&Inf(3)
acc-name: generalized-Buchi 4
$a: 4 Inf(0)&Inf(1)&Inf(2)&Inf(3)
$a: 4 Fin(1) | (Fin(2) & Fin(3) & Fin(0))
$a: 4 Inf(2) | ((Inf(0) | Inf(1)) & Fin(3))
$a: 4 (Fin(0) & Fin(3)) | (Fin(2) & Inf(1))
acc-name: parity min even 4
$a: 4 Inf(0) | (Fin(1) & (Inf(2) | Fin(3)))
acc-name: parity max odd 3
$a: 3 Fin(2) & (Inf(1) | Fin(0))
acc-name: parity max even 3
$a: 3 Inf(2) | (Fin(1) & Inf(0))
acc-name: parity max even 2
$a: 2 Fin(1) & Inf(0)
acc-name: parity max odd 4
$a: 4 Inf(3) | (Fin(2) & (Inf(1) | Fin(0)))
acc-name: generalized-Rabin 3 2 3 0
$a: 8 (Fin(0) & (Inf(1)&Inf(2))) | (Fin(3) & (Inf(4)&Inf(5)&Inf(6))) | Fin(7)
acc-name: generalized-Rabin 3 2 2 0
$a: 7 (Fin(0) & (Inf(1)&Inf(2))) | (Fin(3) & (Inf(4)&Inf(5))) | Fin(6)
acc-name: generalized-Rabin 3 2 3 0
$a: 8 (Fin(0) & (Inf(1)&Inf(2))) | (Fin(3) & (Inf(4)&Inf(5)&Inf(6))) | Fin(7)
acc-name: generalized-Rabin 3 1 2 0
$a: 6 (Fin(0) & Inf(1)) | (Fin(2) & (Inf(3)&Inf(4))) | Fin(5)
acc-name: generalized-Rabin 3 1 3 0
$a: 7 (Fin(0) & Inf(1)) | (Fin(2) & (Inf(3)&Inf(4)&Inf(5))) | Fin(6)
EOF
diff output expected
@ -122,14 +122,14 @@ $randaut -n 10 2..4 -H | grep AP: > output
cat output
cat >expected <<EOF
AP: 3 "p0" "p1" "p2"
AP: 4 "p0" "p1" "p2" "p3"
AP: 3 "p0" "p1" "p2"
AP: 3 "p0" "p1" "p2"
AP: 3 "p0" "p1" "p2"
AP: 4 "p0" "p1" "p2" "p3"
AP: 3 "p0" "p1" "p2"
AP: 4 "p0" "p1" "p2" "p3"
AP: 2 "p0" "p1"
AP: 3 "p0" "p1" "p2"
AP: 2 "p0" "p1"
AP: 4 "p0" "p1" "p2" "p3"
AP: 3 "p0" "p1" "p2"
EOF
diff output expected

File diff suppressed because it is too large Load diff