You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rngxx/src/rng.cpp

58 lines
1.1 KiB

#include <rng.h>
#include <range.h>
template<typename T>
static inline constexpr T _scale(f64 sample, T max)
{
return (T)(sample * (f64)max);
}
bool Random::next_bool() { return sample() < 0.5; }
#define NEXT(T) T Random::next_ ## T(T max) { return _scale<T >(sample(), max); }
#define NEXTT(n) NEXT(i ## n) NEXT(u ## n)
#define N_INTS \
NEXTT(8) \
NEXTT(16) \
NEXTT(32) \
NEXTT(64)
N_INTS
#undef NEXTT
#undef NEXT
// Inefficient sample() based impl of `bytes()`
void Random::next_bytes(u8* ptr, usize n)
{
while( n --> 0 )
*ptr++ = next_u8();
}
// Non-vertorised impl of _v methods for base
void Random::next_v64(u64* p, usize n)
{
return next_bytes(reinterpret_cast<u8*>(p), n * sizeof(u64));
}
void Random::next_v32(u32* p, usize n)
{
return next_bytes(reinterpret_cast<u8*>(p), n * sizeof(u32));
}
// Array getters
#define NEXT(T) void Random::next_ ## T(T* a, usize n) { while( n --> 0 ) *a++ = next_ ## T(); }
#define NEXTT(n) NEXT(i ## n) NEXT(u ## n)
N_INTS
#undef NEXTT
#undef NEXT
//TODO: next_*(min, max)
void __()
{
util::range<int> test(-10, 20);
test.scale({ 100, 200 }, 5);
}