spot  2.2.2
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
random.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2015 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 
74  SPOT_API double bmrand();
75 
87  template<double (*gen)()>
88  class barand
89  {
90  public:
91  barand(int n, double p)
92  : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
93  {
94  }
95 
96  int
97  rand() const
98  {
99  for (;;)
100  {
101  int x = round(gen() * s_ + m_);
102  if (x < 0)
103  continue;
104  if (x <= n_)
105  return x;
106  }
107  SPOT_UNREACHABLE();
108  return 0;
109  }
110  protected:
111  const int n_;
112  const double m_;
113  const double s_;
114  };
115 
120  SPOT_API int prand(double p);
121 
125  template<class iterator_type>
126  SPOT_API void mrandom_shuffle(iterator_type&& first, iterator_type&& last)
127  {
128  auto d = std::distance(first, last);
129  if (d > 1)
130  {
131  for (--last; first < last; ++first, --d)
132  {
133  auto i = mrand(d);
134  std::swap(*first, *(first + i));
135  }
136  }
137  }
139 }
Definition: graph.hh:32
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:126
int rrand(int min, int max)
Compute a pseudo-random integer value between min and max included.
double drand()
Compute a pseudo-random double value between 0.0 and 1.0 (1.0 excluded).
double nrand()
Compute a pseudo-random double value following a standard normal distribution. (Odeh & Evans) ...
void srand(unsigned int seed)
Reset the seed of the pseudo-random number generator.
int mrand(int max)
Compute a pseudo-random integer value between 0 and max-1 included.
double bmrand()
Compute a pseudo-random double value following a standard normal distribution. (Box-Muller) ...
Compute pseudo-random integer value between 0 and n included, following a binomial distribution with ...
Definition: random.hh:88
int prand(double p)
Return a pseudo-random positive integer value following a Poisson distribution with parameter p...

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Dec 16 2016 06:04:08 for spot by doxygen 1.8.8