minstd_rand.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED
  2. #define BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED
  3. // Copyright 2017 Peter Dimov
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. // An implementation of minstd_rand that does not require
  11. // the Random library
  12. #include <boost/cstdint.hpp>
  13. namespace boost
  14. {
  15. namespace detail
  16. {
  17. class minstd_rand
  18. {
  19. private:
  20. boost::uint_least32_t x_;
  21. enum { a = 48271, m = 2147483647 };
  22. public:
  23. minstd_rand(): x_( 1 )
  24. {
  25. }
  26. explicit minstd_rand( boost::uint_least32_t x ): x_( x % m )
  27. {
  28. if( x_ == 0 )
  29. {
  30. x_ = 1;
  31. }
  32. }
  33. boost::uint_least32_t operator()()
  34. {
  35. boost::uint_least64_t y = x_;
  36. y = ( a * y ) % m;
  37. x_ = static_cast<boost::uint_least32_t>( y );
  38. return x_;
  39. }
  40. };
  41. } // namespace detail
  42. } // namespace boost
  43. #endif // #ifndef BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED