| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_RANDOM_ENGINE_HPP |
| 4 |
#define MPT_RANDOM_ENGINE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
#include <random> |
| 12 |
|
| 13 |
#include <cstddef> |
| 14 |
|
| 15 |
|
| 16 |
namespace mpt { |
| 17 |
inline namespace MPT_INLINE_NS { |
| 18 |
|
| 19 |
|
| 20 |
template <typename Trng> |
| 21 |
struct engine_seed_traits { |
| 22 |
static inline constexpr std::size_t seed_bits = Trng::seed_bits; |
| 23 |
}; |
| 24 |
|
| 25 |
template <typename Trng> |
| 26 |
struct engine_traits { |
| 27 |
using result_type = typename Trng::result_type; |
| 28 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 29 |
return Trng::result_bits(); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
// C++11 random does not provide any sane way to determine the amount of entropy |
| 34 |
// required to seed a particular engine. VERY STUPID. |
| 35 |
// List the ones we are likely to use. |
| 36 |
|
| 37 |
template <> |
| 38 |
struct engine_seed_traits<std::mt19937> { |
| 39 |
static inline constexpr std::size_t seed_bits = sizeof(std::mt19937::result_type) * 8 * std::mt19937::state_size; |
| 40 |
}; |
| 41 |
|
| 42 |
template <> |
| 43 |
struct engine_traits<std::mt19937> { |
| 44 |
using rng_type = std::mt19937; |
| 45 |
using result_type = rng_type::result_type; |
| 46 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 47 |
return rng_type::word_size; |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
template <> |
| 52 |
struct engine_seed_traits<std::mt19937_64> { |
| 53 |
static inline constexpr std::size_t seed_bits = sizeof(std::mt19937_64::result_type) * 8 * std::mt19937_64::state_size; |
| 54 |
}; |
| 55 |
|
| 56 |
template <> |
| 57 |
struct engine_traits<std::mt19937_64> { |
| 58 |
using rng_type = std::mt19937_64; |
| 59 |
using result_type = rng_type::result_type; |
| 60 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 61 |
return rng_type::word_size; |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
template <> |
| 66 |
struct engine_seed_traits<std::ranlux24_base> { |
| 67 |
static inline constexpr std::size_t seed_bits = std::ranlux24_base::word_size; |
| 68 |
}; |
| 69 |
|
| 70 |
template <> |
| 71 |
struct engine_traits<std::ranlux24_base> { |
| 72 |
using rng_type = std::ranlux24_base; |
| 73 |
using result_type = rng_type::result_type; |
| 74 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 75 |
return rng_type::word_size; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
template <> |
| 80 |
struct engine_seed_traits<std::ranlux48_base> { |
| 81 |
static inline constexpr std::size_t seed_bits = std::ranlux48_base::word_size; |
| 82 |
}; |
| 83 |
|
| 84 |
template <> |
| 85 |
struct engine_traits<std::ranlux48_base> { |
| 86 |
using rng_type = std::ranlux48_base; |
| 87 |
using result_type = rng_type::result_type; |
| 88 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 89 |
return rng_type::word_size; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
template <> |
| 94 |
struct engine_seed_traits<std::ranlux24> { |
| 95 |
static inline constexpr std::size_t seed_bits = std::ranlux24_base::word_size; |
| 96 |
}; |
| 97 |
|
| 98 |
template <> |
| 99 |
struct engine_traits<std::ranlux24> { |
| 100 |
using rng_type = std::ranlux24; |
| 101 |
using result_type = rng_type::result_type; |
| 102 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 103 |
return std::ranlux24_base::word_size; |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
template <> |
| 108 |
struct engine_seed_traits<std::ranlux48> { |
| 109 |
static inline constexpr std::size_t seed_bits = std::ranlux48_base::word_size; |
| 110 |
}; |
| 111 |
|
| 112 |
template <> |
| 113 |
struct engine_traits<std::ranlux48> { |
| 114 |
using rng_type = std::ranlux48; |
| 115 |
using result_type = rng_type::result_type; |
| 116 |
static MPT_CONSTEXPRINLINE int result_bits() { |
| 117 |
return std::ranlux48_base::word_size; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
} // namespace MPT_INLINE_NS |
| 124 |
} // namespace mpt |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#endif // MPT_RANDOM_ENGINE_HPP |