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-2015 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://image.diku.dk/shark/>
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  * \brief C'tor, initializes the interval the random numbers are sampled from.
55  * \param [in] low_ The lower bound of the interval, defaults to 0.
56  * \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
57  */
58  /*DiscreteUniform( std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
59  :Base(&Rng::globalRng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
60  {}*/
61 
62  /**
63  * \brief C'tor, initializes the interval the random numbers are sampled from and associates the distribution
64  * with the supplied RNG.
65  * \param [in,out] rng The RNG this distribution is associated with.
66  * \param [in] low_ The lower bound of the interval, defaults to 0.
67  * \param [in] high_ The upper bound of the interval, defaults to std::numeric_limits<std::size_t>::max().
68  */
69  DiscreteUniform(RngType & rng, std::size_t low_ = 0,std::size_t high_ = std::numeric_limits<std::size_t>::max() )
70  :Base(&rng,boost::uniform_int<std::size_t>(std::min(low_,high_),std::max(low_,high_)))
71  {}
72 
73  /**
74  * \brief Injects the default sampling operator.
75  */
76  using Base::operator();
77 
78  /**
79  * \brief Reinitializes the distribution for the supplied bounds and samples a new random number.
80  * Default values are omitted to distinguish the operator from the default one.
81  *
82  * \param [in] low_ The lower bound of the interval.
83  * \param [in] high_ The upper bound of the interval.
84  */
85  typename Base::result_type operator()(std::size_t low_,std::size_t high_)
86  {
87  boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
88  return dist(Base::engine());
89  }
90 
91  /** \brief Returns the lower bound of the interval. */
92  std::size_t low()const
93  {
94  return Base::distribution().min();
95  }
96 
97  /** \brief Adjusts the upper bound of the interval */
98  std::size_t high()const
99  {
100  return Base::distribution().max();
101  }
102 
103  /** \brief Adjusts the range of the interval. */
104  void setRange(std::size_t low_,std::size_t high_)
105  {
106  boost::uniform_int<std::size_t> dist(std::min(low_,high_),std::max(low_,high_));
107  Base::distribution()=dist;
108  }
109 
110  /** \brief Calculates the probability of x. */
111  double p( std::size_t x ) const {
112  return 1.0/(high()-low()+1);
113  }
114 
115  };
116 
117  template<typename RngType>
118  double entropy(const DiscreteUniform<RngType> & uniform);
119 
120 }
121 #endif