spot  2.12.2
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 
26 namespace 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 
76  template<double (*gen)()>
77  class barand
78  {
79  public:
80  barand(int n, double p)
81  : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p)))
82  {
83  }
84 
85  int
86  rand() const
87  {
88  for (;;)
89  {
90  int x = round(gen() * s_ + m_);
91  if (x < 0)
92  continue;
93  if (x <= n_)
94  return x;
95  }
96  SPOT_UNREACHABLE();
97  return 0;
98  }
99  protected:
100  const int n_;
101  const double m_;
102  const double s_;
103  };
104 
108  template<class iterator_type>
109  SPOT_API void mrandom_shuffle(iterator_type&& first, iterator_type&& last)
110  {
111  auto d = std::distance(first, last);
112  if (d > 1)
113  {
114  for (--last; first < last; ++first, --d)
115  {
116  auto i = mrand(d);
117  std::swap(*first, *(first + i));
118  }
119  }
120  }
122 }
Compute pseudo-random integer value between 0 and n included, following a binomial distribution with ...
Definition: random.hh:78
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:109
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.1