catch_random_number_generator.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Created by Martin on 30/08/2017.
  3. *
  4. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
  8. #define TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED
  9. #include <cstdint>
  10. namespace Catch {
  11. // This is a simple implementation of C++11 Uniform Random Number
  12. // Generator. It does not provide all operators, because Catch2
  13. // does not use it, but it should behave as expected inside stdlib's
  14. // distributions.
  15. // The implementation is based on the PCG family (http://pcg-random.org)
  16. class SimplePcg32 {
  17. using state_type = std::uint64_t;
  18. public:
  19. using result_type = std::uint32_t;
  20. static constexpr result_type (min)() {
  21. return 0;
  22. }
  23. static constexpr result_type (max)() {
  24. return static_cast<result_type>(-1);
  25. }
  26. // Provide some default initial state for the default constructor
  27. SimplePcg32():SimplePcg32(0xed743cc4U) {}
  28. explicit SimplePcg32(result_type seed_);
  29. void seed(result_type seed_);
  30. void discard(uint64_t skip);
  31. result_type operator()();
  32. private:
  33. friend bool operator==(SimplePcg32 const& lhs, SimplePcg32 const& rhs);
  34. friend bool operator!=(SimplePcg32 const& lhs, SimplePcg32 const& rhs);
  35. // In theory we also need operator<< and operator>>
  36. // In practice we do not use them, so we will skip them for now
  37. std::uint64_t m_state;
  38. // This part of the state determines which "stream" of the numbers
  39. // is chosen -- we take it as a constant for Catch2, so we only
  40. // need to deal with seeding the main state.
  41. // Picked by reading 8 bytes from `/dev/random` :-)
  42. static const std::uint64_t s_inc = (0x13ed0cc53f939476ULL << 1ULL) | 1ULL;
  43. };
  44. } // end namespace Catch
  45. #endif // TWOBLUECUBES_CATCH_RANDOM_NUMBER_GENERATOR_H_INCLUDED