DiscreteUniform.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Discrete Uniform distribution
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2010-01-01
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_RNG_DISCRETEUNIFORM_H
33 #define SHARK_RNG_DISCRETEUNIFORM_H
34 
35 #include <shark/Rng/Rng.h>
36 
37 #include <boost/random.hpp>
38 
39 #include <algorithm>
40 #include <cmath>
41 #include <limits>
42 
43 namespace shark{
44 /**
45 * \brief Implements the discrete uniform distribution.
46 */
47 template<typename RngType = shark::DefaultRngType>
48 class DiscreteUniform : public boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > {
49 private:
50  /** \brief The base type this class inherits from. */
51  typedef boost::variate_generator<RngType*,boost::uniform_int<std::size_t> > Base;
52 public:
53 
54  /**
55  * \brief C'tor, initializes the interval the random numbers are sampled from and associates the distribution
56  * with the supplied RNG.
57  * \param [in,out] rng The RNG this distribution is associated with.
58  * \param [in] low_ The lower bound of the interval, defaults to 0.
59  * \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
60  */
61  DiscreteUniform(RngType & rng, std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
62  :Base(&rng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
63  {}
64 
65  /**
66  * \brief Injects the default sampling operator.
67  */
68  using Base::operator();
69 
70  /**
71  * \brief Reinitializes the distribution for the supplied bounds and samples a new random number.
72  * Default values are omitted to distinguish the operator from the default one.
73  *
74  * \param [in] low_ The lower bound of the interval.
75  * \param [in] high_ The upper bound of the interval.
76  */
77  typename Base::result_type operator()(std::size_t low_,std::size_t high_)
78  {
79  boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
80  return dist(Base::engine());
81  }
82 
83  /** \brief Returns the lower bound of the interval. */
84  std::size_t low()const
85  {
86  return Base::distribution().min();
87  }
88 
89  /** \brief Adjusts the upper bound of the interval */
90  std::size_t high()const
91  {
92  return Base::distribution().max();
93  }
94 
95  /** \brief Adjusts the range of the interval. */
96  void setRange(std::size_t low_,std::size_t high_)
97  {
98  boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
99  Base::distribution()=dist;
100  }
101 
102  /** \brief Calculates the probability of x. */
103  double p( std::size_t x ) const {
104  return 1.0/(high()-low()+1);
105  }
106 
107 };
108 
109 ///\brief Draws a discrete number in {low,low+1,...,high} by drawing random numbers from rng.
110 template<class RngType>
111 std::size_t discrete(RngType& rng, std::size_t low, std::size_t high){
112  DiscreteUniform<RngType> dist(rng, low, high);
113  return dist();
114 }
115 
116 }
117 #endif