Brian Silverman | 60e3e2a | 2018-08-04 23:57:12 -0700 | [diff] [blame^] | 1 | #ifndef BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED |
| 2 | #define BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED |
| 3 | |
| 4 | // Copyright 2017 Peter Dimov |
| 5 | // |
| 6 | // Distributed under the Boost Software License, Version 1.0. |
| 7 | // |
| 8 | // See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt |
| 10 | // |
| 11 | // An implementation of minstd_rand that does not require |
| 12 | // the Random library |
| 13 | |
| 14 | #include <boost/cstdint.hpp> |
| 15 | |
| 16 | namespace boost |
| 17 | { |
| 18 | namespace detail |
| 19 | { |
| 20 | |
| 21 | class minstd_rand |
| 22 | { |
| 23 | private: |
| 24 | |
| 25 | boost::uint_least32_t x_; |
| 26 | |
| 27 | enum { a = 48271, m = 2147483647 }; |
| 28 | |
| 29 | public: |
| 30 | |
| 31 | minstd_rand(): x_( 1 ) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | explicit minstd_rand( boost::uint_least32_t x ): x_( x % m ) |
| 36 | { |
| 37 | if( x_ == 0 ) |
| 38 | { |
| 39 | x_ = 1; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | boost::uint_least32_t operator()() |
| 44 | { |
| 45 | boost::uint_least64_t y = x_; |
| 46 | |
| 47 | y = ( a * y ) % m; |
| 48 | |
| 49 | x_ = static_cast<boost::uint_least32_t>( y ); |
| 50 | |
| 51 | return x_; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | } // namespace detail |
| 56 | } // namespace boost |
| 57 | |
| 58 | #endif // #ifndef BOOST_UTILITY_DETAIL_MINSTD_RAND_HPP_INCLUDED |