1 ///////////////////////////////////////////////////////////////
2 // Copyright 2018 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
6 #include <boost/multiprecision/cpp_int.hpp>
11 typedef std::uint64_t result_type
;
13 constexpr kiss_rand() : x(0x8207ebe160468b32uLL
), y(0x2871283e01d45bbduLL
), z(0x9c80bfd5db9680c9uLL
), c(0x2e2683c2abb878b8uLL
) {}
14 constexpr kiss_rand(std::uint64_t seed
) : x(seed
), y(0x2871283e01d45bbduLL
), z(0x9c80bfd5db9680c9uLL
), c(0x2e2683c2abb878b8uLL
) {}
15 constexpr kiss_rand(std::uint64_t seed_x
, std::uint64_t seed_y
) : x(seed_x
), y(seed_y
), z(0x9c80bfd5db9680c9uLL
), c(0x2e2683c2abb878b8uLL
) {}
16 constexpr kiss_rand(std::uint64_t seed_x
, std::uint64_t seed_y
, std::uint64_t seed_z
) : x(seed_x
), y(seed_y
), z(seed_z
), c(0x2e2683c2abb878b8uLL
) {}
18 constexpr std::uint64_t operator()()
20 return MWC() + XSH() + CNG();
24 constexpr std::uint64_t MWC()
26 std::uint64_t t
= (x
<< 58) + c
;
32 constexpr std::uint64_t XSH()
36 return y
^= (y
<< 43);
38 constexpr std::uint64_t CNG()
40 return z
= 6906969069LL * z
+ 1234567;
42 std::uint64_t x
, y
, z
, c
;
45 inline constexpr void hash_combine(std::uint64_t& h
, std::uint64_t k
)
47 constexpr const std::uint64_t m
= 0xc6a4a7935bd1e995uLL
;
48 constexpr const int r
= 47;
57 // Completely arbitrary number, to prevent 0's
62 template <std::size_t N
>
63 inline constexpr std::uint64_t string_to_hash(const char (&s
)[N
])
65 std::uint64_t hash(0);
66 for (unsigned i
= 0; i
< N
; ++i
)
67 hash_combine(hash
, s
[i
]);
71 template <class UnsignedInteger
>
72 struct multiprecision_generator
74 typedef UnsignedInteger result_type
;
75 constexpr multiprecision_generator(std::uint64_t seed1
) : m_gen64(seed1
) {}
76 constexpr multiprecision_generator(std::uint64_t seed1
, std::uint64_t seed2
) : m_gen64(seed1
, seed2
) {}
77 constexpr multiprecision_generator(std::uint64_t seed1
, std::uint64_t seed2
, std::uint64_t seed3
) : m_gen64(seed1
, seed2
, seed3
) {}
79 static constexpr result_type (min
)()
83 static constexpr result_type (max
)()
85 return ~result_type(0u);
87 constexpr result_type
operator()()
89 result_type
result(m_gen64());
91 while (digits
< std::numeric_limits
<result_type
>::digits
)
104 template <class UnsignedInteger
>
105 constexpr UnsignedInteger
nth_random_value(unsigned count
= 0)
107 std::uint64_t date_hash
= string_to_hash(__DATE__
);
108 std::uint64_t time_hash
= string_to_hash(__TIME__
);
109 multiprecision_generator
<UnsignedInteger
> big_gen(date_hash
, time_hash
);
110 for (unsigned i
= 0; i
< count
; ++i
)
117 using namespace boost::multiprecision
;
119 constexpr uint1024_t rand
= nth_random_value
<uint1024_t
>(1000);
120 std::cout
<< std::hex
<< rand
<< std::endl
;