uniform_int.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* boost random/uniform_int.hpp header file
  2. *
  3. * Copyright Jens Maurer 2000-2001
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id$
  11. *
  12. * Revision history
  13. * 2001-04-08 added min<max assertion (N. Becker)
  14. * 2001-02-18 moved to individual header files
  15. */
  16. #ifndef BOOST_RANDOM_UNIFORM_INT_HPP
  17. #define BOOST_RANDOM_UNIFORM_INT_HPP
  18. #include <boost/assert.hpp>
  19. #include <boost/random/uniform_int_distribution.hpp>
  20. namespace boost {
  21. /**
  22. * The distribution function uniform_int models a \random_distribution.
  23. * On each invocation, it returns a random integer value uniformly
  24. * distributed in the set of integer numbers {min, min+1, min+2, ..., max}.
  25. *
  26. * The template parameter IntType shall denote an integer-like value type.
  27. *
  28. * This class is deprecated. Please use @c uniform_int_distribution in
  29. * new code.
  30. */
  31. template<class IntType = int>
  32. class uniform_int : public random::uniform_int_distribution<IntType>
  33. {
  34. typedef random::uniform_int_distribution<IntType> base_type;
  35. public:
  36. class param_type : public base_type::param_type
  37. {
  38. public:
  39. typedef uniform_int distribution_type;
  40. /**
  41. * Constructs the parameters of a uniform_int distribution.
  42. *
  43. * Requires: min <= max
  44. */
  45. explicit param_type(IntType min_arg = 0, IntType max_arg = 9)
  46. : base_type::param_type(min_arg, max_arg)
  47. {}
  48. };
  49. /**
  50. * Constructs a uniform_int object. @c min and @c max are
  51. * the parameters of the distribution.
  52. *
  53. * Requires: min <= max
  54. */
  55. explicit uniform_int(IntType min_arg = 0, IntType max_arg = 9)
  56. : base_type(min_arg, max_arg)
  57. {}
  58. /** Constructs a uniform_int distribution from its parameters. */
  59. explicit uniform_int(const param_type& parm)
  60. : base_type(parm)
  61. {}
  62. /** Returns the parameters of the distribution */
  63. param_type param() const { return param_type(this->a(), this->b()); }
  64. /** Sets the parameters of the distribution. */
  65. void param(const param_type& parm) { this->base_type::param(parm); }
  66. // Codergear seems to have trouble with a using declaration here
  67. template<class Engine>
  68. IntType operator()(Engine& eng) const
  69. {
  70. return static_cast<const base_type&>(*this)(eng);
  71. }
  72. template<class Engine>
  73. IntType operator()(Engine& eng, const param_type& parm) const
  74. {
  75. return static_cast<const base_type&>(*this)(eng, parm);
  76. }
  77. template<class Engine>
  78. IntType operator()(Engine& eng, IntType n) const
  79. {
  80. BOOST_ASSERT(n > 0);
  81. return static_cast<const base_type&>(*this)(eng, param_type(0, n - 1));
  82. }
  83. };
  84. } // namespace boost
  85. #endif // BOOST_RANDOM_UNIFORM_INT_HPP