Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions include/REX/TRandom.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ namespace REX
class TRandom
{
public:
using result_type = std::uint64_t;

TRandom();
TRandom(std::uint32_t a_seed);
TRandom(std::uint64_t a_seed);

static constexpr T min() { return std::numeric_limits<T>::min(); }
static constexpr T max() { return std::numeric_limits<T>::max(); }
static constexpr result_type min() noexcept { return std::numeric_limits<result_type>::min(); }
static constexpr result_type max() noexcept { return std::numeric_limits<result_type>::max(); }
result_type operator()() noexcept;

T Generate(T a_min = min(), T a_max = max());
T Generate(T a_min = std::numeric_limits<T>::min(), T a_max = std::numeric_limits<T>::max());

protected:
std::byte m_rng[32];
Expand All @@ -29,13 +32,16 @@ namespace REX
class TRandomDistribution
{
public:
using result_type = std::uint64_t;

TRandomDistribution() = delete;
TRandomDistribution(std::vector<std::uint32_t>& a_weights);
TRandomDistribution(std::uint32_t a_seed, std::vector<std::uint32_t>& a_weights);
TRandomDistribution(std::uint64_t a_seed, std::vector<std::uint32_t>& a_weights);

static constexpr T min() { return std::numeric_limits<T>::min(); }
static constexpr T max() { return std::numeric_limits<T>::max(); }
static constexpr result_type min() noexcept { return std::numeric_limits<result_type>::min(); }
static constexpr result_type max() noexcept { return std::numeric_limits<result_type>::max(); }
result_type operator()() noexcept;

T Generate();

Expand Down
21 changes: 21 additions & 0 deletions src/REX/TRandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
std::memcpy(&m_rng, &rng, 32); \
} \
template <> \
TRandom<T>::result_type TRandom<T>::operator()() noexcept \
{ \
return (*reinterpret_cast<XoshiroCpp::Xoshiro256StarStar*>(m_rng))(); \
} \
template <> \
T TRandom<T>::Generate(T a_min, T a_max) \
{ \
std::uniform_##KIND##_distribution<T> dist(a_min, a_max); \
Expand Down Expand Up @@ -53,6 +58,11 @@
std::memcpy(&m_rng, &rng, sizeof(m_rng)); \
} \
template <> \
TRandomDistribution<T>::result_type TRandomDistribution<T>::operator()() noexcept \
{ \
return (*reinterpret_cast<XoshiroCpp::Xoshiro256StarStar*>(m_rng))(); \
} \
template <> \
T TRandomDistribution<T>::Generate() \
{ \
return m_dist(*reinterpret_cast<XoshiroCpp::Xoshiro256StarStar*>(m_rng)); \
Expand All @@ -70,6 +80,17 @@ namespace REX
REX_DEFINE_RANDOM_DIST_FUNCTIONS(std::int64_t);
REX_DEFINE_RANDOM_DIST_FUNCTIONS(std::uint32_t);
REX_DEFINE_RANDOM_DIST_FUNCTIONS(std::uint64_t);

static_assert(std::uniform_random_bit_generator<RNG::F32>);
static_assert(std::uniform_random_bit_generator<RNG::F64>);
static_assert(std::uniform_random_bit_generator<RNG::I32>);
static_assert(std::uniform_random_bit_generator<RNG::I64>);
static_assert(std::uniform_random_bit_generator<RNG::U32>);
static_assert(std::uniform_random_bit_generator<RNG::U64>);
static_assert(std::uniform_random_bit_generator<RNG::I32D>);
static_assert(std::uniform_random_bit_generator<RNG::I64D>);
static_assert(std::uniform_random_bit_generator<RNG::U32D>);
static_assert(std::uniform_random_bit_generator<RNG::U64D>);
}

# undef XOSHIROCPP_NODISCARD_CXX20
Expand Down
Loading