random_number_generator.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* boost random/random_number_generator.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-02-18 moved to individual header files
  14. */
  15. #ifndef BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP
  16. #define BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP
  17. #include <boost/assert.hpp>
  18. #include <boost/random/uniform_int_distribution.hpp>
  19. #include <boost/random/detail/disable_warnings.hpp>
  20. namespace boost {
  21. namespace random {
  22. /**
  23. * Instantiations of class template random_number_generator model a
  24. * RandomNumberGenerator (std:25.2.11 [lib.alg.random.shuffle]). On
  25. * each invocation, it returns a uniformly distributed integer in
  26. * the range [0..n).
  27. *
  28. * The template parameter IntType shall denote some integer-like value type.
  29. */
  30. template<class URNG, class IntType = long>
  31. class random_number_generator
  32. {
  33. public:
  34. typedef URNG base_type;
  35. typedef IntType argument_type;
  36. typedef IntType result_type;
  37. /**
  38. * Constructs a random_number_generator functor with the given
  39. * \uniform_random_number_generator as the underlying source of
  40. * random numbers.
  41. */
  42. random_number_generator(base_type& rng) : _rng(rng) {}
  43. // compiler-generated copy ctor is fine
  44. // assignment is disallowed because there is a reference member
  45. /**
  46. * Returns a value in the range [0, n)
  47. */
  48. result_type operator()(argument_type n)
  49. {
  50. BOOST_ASSERT(n > 0);
  51. return uniform_int_distribution<IntType>(0, n-1)(_rng);
  52. }
  53. private:
  54. base_type& _rng;
  55. };
  56. } // namespace random
  57. using random::random_number_generator;
  58. } // namespace boost
  59. #include <boost/random/detail/enable_warnings.hpp>
  60. #endif // BOOST_RANDOM_RANDOM_NUMBER_GENERATOR_HPP