Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:06

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_RANDOM_NUMBER_GENERATOR_HPP_INCLUDED
0009 #define CATCH_RANDOM_NUMBER_GENERATOR_HPP_INCLUDED
0010 
0011 #include <cstdint>
0012 
0013 namespace Catch {
0014 
0015     // This is a simple implementation of C++11 Uniform Random Number
0016     // Generator. It does not provide all operators, because Catch2
0017     // does not use it, but it should behave as expected inside stdlib's
0018     // distributions.
0019     // The implementation is based on the PCG family (http://pcg-random.org)
0020     class SimplePcg32 {
0021         using state_type = std::uint64_t;
0022     public:
0023         using result_type = std::uint32_t;
0024         static constexpr result_type (min)() {
0025             return 0;
0026         }
0027         static constexpr result_type (max)() {
0028             return static_cast<result_type>(-1);
0029         }
0030 
0031         // Provide some default initial state for the default constructor
0032         SimplePcg32():SimplePcg32(0xed743cc4U) {}
0033 
0034         explicit SimplePcg32(result_type seed_);
0035 
0036         void seed(result_type seed_);
0037         void discard(uint64_t skip);
0038 
0039         result_type operator()();
0040 
0041     private:
0042         friend bool operator==(SimplePcg32 const& lhs, SimplePcg32 const& rhs);
0043         friend bool operator!=(SimplePcg32 const& lhs, SimplePcg32 const& rhs);
0044 
0045         // In theory we also need operator<< and operator>>
0046         // In practice we do not use them, so we will skip them for now
0047 
0048 
0049         std::uint64_t m_state;
0050         // This part of the state determines which "stream" of the numbers
0051         // is chosen -- we take it as a constant for Catch2, so we only
0052         // need to deal with seeding the main state.
0053         // Picked by reading 8 bytes from `/dev/random` :-)
0054         static const std::uint64_t s_inc = (0x13ed0cc53f939476ULL << 1ULL) | 1ULL;
0055     };
0056 
0057 } // end namespace Catch
0058 
0059 #endif // CATCH_RANDOM_NUMBER_GENERATOR_HPP_INCLUDED