spot  2.11.6
random.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2015, 2017 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
4 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
5 // département Systèmes Répartis Coopératifs (SRC), Université Pierre
6 // et Marie Curie.
7 //
8 // This file is part of Spot, a model checking library.
9 //
10 // Spot is free software; you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Spot is distributed in the hope that it will be useful, but WITHOUT
16 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 // License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 #pragma once
24 
25 #include <spot/misc/common.hh>
26 #include <cassert>
27 #include <cmath>
28 #include <vector>
29 
30 namespace spot
31 {
34 
37 
41  SPOT_API void srand(unsigned int seed);
42 
47  SPOT_API int rrand(int min, int max);
48 
53  SPOT_API int mrand(int max);
54 
59  SPOT_API double drand();
60 
67  SPOT_API double nrand();
68 
80  template<double (*gen)()>
81  class barand
82  {
83  public:
84  barand(int n, double p)
85  : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
86  {
87  }
88 
89  int
90  rand() const
91  {
92  for (;;)
93  {
94  int x = round(gen() * s_ + m_);
95  if (x < 0)
96  continue;
97  if (x <= n_)
98  return x;
99  }
100  SPOT_UNREACHABLE();
101  return 0;
102  }
103  protected:
104  const int n_;
105  const double m_;
106  const double s_;
107  };
108 
112  template<class iterator_type>
113  SPOT_API void mrandom_shuffle(iterator_type&& first, iterator_type&& last)
114  {
115  auto d = std::distance(first, last);
116  if (d > 1)
117  {
118  for (--last; first < last; ++first, --d)
119  {
120  auto i = mrand(d);
121  std::swap(*first, *(first + i));
122  }
123  }
124  }
126 }
Compute pseudo-random integer value between 0 and n included, following a binomial distribution with ...
Definition: random.hh:82
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:113
int mrand(int max)
Compute a pseudo-random integer value between 0 and max-1 included.
Definition: automata.hh:27

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.1