63 lines
1.7 KiB
C++

#include <cinttypes>
#include <algorithm>
#include "values/mrand.hpp"
static inline uint32_t my_rotate_left(const uint32_t x, int k)
{
return (x << k) | (x >> (32 - k));
}
uint32_t my_rprand_xoshiro(uint32_t state[4])
{
const uint32_t result = my_rotate_left(state[1] * 5, 7) * 9;
const uint32_t t = state[1] << 9;
state[2] ^= state[0];
state[3] ^= state[1];
state[1] ^= state[2];
state[0] ^= state[3];
state[2] ^= t;
state[3] = my_rotate_left(state[3], 11);
return result;
}
uint64_t rprand_splitmix64(uint64_t &rprand_seed)
{
uint64_t z = (rprand_seed += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
namespace mrand
{
uint128 getState(unsigned long long seed)
{
uint64_t rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
uint128 rprand_state;
// To generate the Xoshiro128** state, we use SplitMix64 generator first
// We generate 4 pseudo-random 64bit numbers that we combine using their LSB|MSB
rprand_state.a = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state.b = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
rprand_state.c = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state.d = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
return rprand_state;
}
int getValue(int min, int max, uint128 *state)
{
int value = my_rprand_xoshiro((uint32_t *)state) % (std::abs(max - min) + 1) + min;
return value;
}
float getFloat(uint128 *state)
{
return my_rprand_xoshiro((uint32_t *)state) / 4294967295.0f;
}
}