-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add boost/core/detail/minstd_rand.hpp
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef BOOST_CORE_DETAIL_MINSTD_RAND_HPP_INCLUDED | ||
#define BOOST_CORE_DETAIL_MINSTD_RAND_HPP_INCLUDED | ||
|
||
// Copyright 2017 Peter Dimov | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// | ||
// See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt | ||
// | ||
// An implementation of minstd_rand that does not require | ||
// the Random library | ||
|
||
#include <boost/cstdint.hpp> | ||
|
||
namespace boost | ||
{ | ||
namespace detail | ||
{ | ||
|
||
class minstd_rand | ||
{ | ||
private: | ||
|
||
boost::uint_least32_t x_; | ||
|
||
enum { a = 48271, m = 2147483647 }; | ||
|
||
public: | ||
|
||
minstd_rand(): x_( 1 ) | ||
{ | ||
} | ||
|
||
explicit minstd_rand( boost::uint_least32_t x ): x_( x % m ) | ||
{ | ||
if( x_ == 0 ) | ||
{ | ||
x_ = 1; | ||
} | ||
} | ||
|
||
boost::uint_least32_t operator()() | ||
{ | ||
boost::uint_least64_t y = x_; | ||
|
||
y = ( a * y ) % m; | ||
|
||
x_ = static_cast<boost::uint_least32_t>( y ); | ||
|
||
return x_; | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
} // namespace boost | ||
|
||
#endif // #ifndef BOOST_CORE_DETAIL_MINSTD_RAND_HPP_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Test for boost/core/detail/minstd_rand.hpp | ||
// | ||
// Copyright 2022, 2024 Peter Dimov | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// https://www.boost.org/LICENSE_1_0.txt | ||
|
||
#include <boost/core/detail/minstd_rand.hpp> | ||
#include <boost/core/lightweight_test.hpp> | ||
#include <boost/cstdint.hpp> | ||
|
||
int main() | ||
{ | ||
{ | ||
boost::detail::minstd_rand rng; | ||
|
||
boost::uint_least32_t r1 = rng(), x1 = 48271; | ||
BOOST_TEST_EQ( r1, x1 ); | ||
|
||
for( int i = 0; i < 1000; ++i ) rng(); | ||
|
||
boost::uint_least32_t r2 = rng(), x2 = 2076422031; | ||
BOOST_TEST_EQ( r2, x2 ); | ||
} | ||
|
||
{ | ||
boost::detail::minstd_rand rng( 12345 ); | ||
|
||
boost::uint_least32_t r1 = rng(), x1 = 595905495; | ||
BOOST_TEST_EQ( r1, x1 ); | ||
|
||
for( int i = 0; i < 1000; ++i ) rng(); | ||
|
||
boost::uint_least32_t r2 = rng(), x2 = 1065162103; | ||
BOOST_TEST_EQ( r2, x2 ); | ||
} | ||
|
||
return boost::report_errors(); | ||
} |