From f774295cdbaf3a845ad1bdf8fcf228f0d49c44bd Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 14 Apr 2024 04:40:44 +0300 Subject: [PATCH 1/2] Add boost/core/detail/minstd_rand.hpp --- include/boost/core/detail/minstd_rand.hpp | 58 +++++++++++++++++++++++ test/Jamfile.v2 | 3 ++ test/minstd_rand_test.cpp | 38 +++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 include/boost/core/detail/minstd_rand.hpp create mode 100644 test/minstd_rand_test.cpp diff --git a/include/boost/core/detail/minstd_rand.hpp b/include/boost/core/detail/minstd_rand.hpp new file mode 100644 index 0000000..bedafb2 --- /dev/null +++ b/include/boost/core/detail/minstd_rand.hpp @@ -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 + +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( y ); + + return x_; + } +}; + +} // namespace detail +} // namespace boost + +#endif // #ifndef BOOST_CORE_DETAIL_MINSTD_RAND_HPP_INCLUDED diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index e05352c..5df9242 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -440,5 +440,8 @@ run yield_prim_pthread_cancel_test.cpp : ; run pointer_in_range_test.cpp ; compile pointer_in_range_constexpr_test.cpp ; +run minstd_rand_test.cpp + : : : $(pedantic-errors) ; + use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/minstd_rand_test.cpp b/test/minstd_rand_test.cpp new file mode 100644 index 0000000..deb67e9 --- /dev/null +++ b/test/minstd_rand_test.cpp @@ -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 +#include +#include + +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(); +} From d03e58b77e54ac1c18f7fff049994bdc36eea482 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Thu, 18 Apr 2024 13:16:12 -0400 Subject: [PATCH 2/2] Correct parameter type in documentation --- doc/pointer_in_range.qbk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/pointer_in_range.qbk b/doc/pointer_in_range.qbk index 7da7b6d..066994f 100644 --- a/doc/pointer_in_range.qbk +++ b/doc/pointer_in_range.qbk @@ -56,7 +56,7 @@ constexpr bool pointer_in_range(const T* ptr, const T* begin, const T* end); [variablelist [[`template constexpr bool pointer_in_range(const T* ptr, -const T* begin, T* end);`] +const T* begin, const T* end);`] [[variablelist [[Requires][`[begin,end)` is a valid range.]] [[Returns][`true` if `ptr` is in range `[begin,end)`, otherwise `false`.]]]]]]