spot 2.16
Loading...
Searching...
No Matches
random.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) by the Spot authors, see the AUTHORS file for details.
3//
4// This file is part of Spot, a model checking library.
5//
6// Spot is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// Spot is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14// License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#pragma once
20
21#include <spot/misc/common.hh>
22#include <cassert>
23#include <cmath>
24#include <vector>
25
26namespace spot
27{
30
33
37 SPOT_API void srand(unsigned int seed);
38
43 SPOT_API int rrand(int min, int max);
44
49 SPOT_API int mrand(int max);
50
55 SPOT_API double drand();
56
63 SPOT_API double nrand();
64
77 template<double (*gen)()>
78 class barand
79 {
80 public:
82 barand(int n, double p)
83 : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
84 {
85 }
86
88 int
89 rand() const
90 {
91 for (;;)
92 {
93 int x = round(gen() * s_ + m_);
94 if (x < 0)
95 continue;
96 if (x <= n_)
97 return x;
98 }
99 SPOT_UNREACHABLE();
100 return 0;
101 }
102 protected:
103 const int n_;
104 const double m_;
105 const double s_;
106 };
107
111 template<class iterator_type>
112 SPOT_API void mrandom_shuffle(iterator_type&& first, iterator_type&& last)
113 {
114 auto d = std::distance(first, last);
115 if (d > 1)
116 {
117 for (--last; first < last; ++first, --d)
118 {
119 auto i = mrand(d);
120 std::swap(*first, *(first + i));
121 }
122 }
123 }
125}
Compute pseudo-random integer value between 0 and n included, following a binomial distribution with ...
Definition random.hh:79
const int n_
Upper bound.
Definition random.hh:103
const double s_
Standard deviation.
Definition random.hh:105
const double m_
Mean.
Definition random.hh:104
barand(int n, double p)
Build a binomial random generator.
Definition random.hh:82
int rand() const
Return a random value.
Definition random.hh:89
double drand()
Compute a pseudo-random double value between 0.0 and 1.0 (1.0 excluded).
void srand(unsigned int seed)
Reset the seed of the pseudo-random number generator.
int rrand(int min, int max)
Compute a pseudo-random integer value between min and max included.
double nrand()
Compute a pseudo-random double value following a standard normal distribution. (Odeh & Evans)
void mrandom_shuffle(iterator_type &&first, iterator_type &&last)
Shuffle the container using mrand function above. This allows to get rid off shuffle or random_shuffl...
Definition random.hh:112
int mrand(int max)
Compute a pseudo-random integer value between 0 and max-1 included.
Definition automata.hh:26

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.8