Random.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Shark Random number generation
5  *
6  *
7  *
8  * \author O.Krause
9  * \date 2017
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_CORE_RANDOM_H
33 #define SHARK_CORE_RANDOM_H
34 
35 #include <random>
36 #include <shark/Core/DLLSupport.h>
37 
38 namespace shark{
39 namespace random{
40 
41  /** \brief Default RNG of the shark library. */
42  typedef std::mt19937 rng_type;
43 
44  SHARK_EXPORT_SYMBOL extern rng_type globalRng;
45 
46  ///\brief Flips a coin with probability of heads being pHeads by drawing random numbers from rng.
47  template<class RngType>
48  bool coinToss(RngType& rng, double pHeads = 0.5){
49  std::bernoulli_distribution dist(pHeads);
50  return dist(rng);
51  }
52 
53  ///\brief Draws a number uniformly in [lower,upper] by drawing random numbers from rng.
54  template<class RngType>
55  double uni(RngType& rng, double lower = 0.0, double upper = 1.0){
56  std::uniform_real_distribution<double> dist(lower,upper);
57  return dist(rng);
58  }
59 
60  ///\brief Draws a discrete number in {low,low+1,...,high} by drawing random numbers from rng.
61  template<class RngType, class T>
62  T discrete(RngType& rng, T low, T high){
63  std::uniform_int_distribution<T> dist(low, high);
64  return dist(rng);
65  }
66 
67  ///\brief Draws a number from the normal distribution with given mean and variance by drawing random numbers from rng.
68  template<class RngType>
69  double gauss(RngType& rng, double mean = 0.0, double variance = 1.0){
70  std::normal_distribution<double> dist(mean,std::sqrt(variance));
71  return dist(rng);
72  }
73 
74  ///\brief Draws a number from the log-normal distribution as exp(gauss(m,v))
75  template<class RngType>
76  double logNormal(RngType& rng, double m = 0.0, double v = 1.0){
77  return std::exp(gauss(rng,m,v));
78  }
79 
80  ///\brief draws a number from the truncated exponential distribution
81  ///
82  /// draws from the exponential distribution p(x|lambda)= 1/Z exp(-lambda*x) subject to x < maximum
83  /// as optuonal third argument it is possible to return the precomputed value of Z.
84  template<class RngType>
85  double truncExp(RngType& rng, double lambda, double maximum, double Z = -1.0){
86  double y = uni(rng,0.0,maximum);
87  if(lambda == 0){
88  return y;
89  }
90  if(Z < 0)
91  Z = 1-std::exp(-lambda*maximum);
92  return - std::log(1. - y*Z)/lambda;
93  }
94 
95 
96 
97 
98 }}
99 
100 #endif