Add random generator that accepts state for repetability
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
static uint32_t rprand_state[4] = {0};
|
||||
|
||||
static inline uint32_t my_rotate_left(const uint32_t x, int k)
|
||||
{
|
||||
return (x << k) | (x >> (32 - k));
|
||||
}
|
||||
|
||||
uint32_t my_rprand_xoshiro(void)
|
||||
{
|
||||
const uint32_t result = my_rotate_left(rprand_state[1] * 5, 7) * 9;
|
||||
const uint32_t t = rprand_state[1] << 9;
|
||||
|
||||
rprand_state[2] ^= rprand_state[0];
|
||||
rprand_state[3] ^= rprand_state[1];
|
||||
rprand_state[1] ^= rprand_state[2];
|
||||
rprand_state[0] ^= rprand_state[3];
|
||||
|
||||
rprand_state[2] ^= t;
|
||||
|
||||
rprand_state[3] = my_rotate_left(rprand_state[3], 11);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "values/RandBuffer.hpp"
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
void RandBuffer::reset()
|
||||
{
|
||||
@@ -13,7 +13,7 @@ float RandBuffer::next()
|
||||
{
|
||||
return ((float *)m_buffer.buffer)[m_pos++];
|
||||
}
|
||||
float ret = GetRandomFloat();
|
||||
float ret = mrand::getFloat();
|
||||
m_buffer.add_end((uint8_t *)&ret, sizeof(float));
|
||||
m_pos++;
|
||||
return ret;
|
||||
|
||||
63
src/values/mrand.cpp
Normal file
63
src/values/mrand.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <inttypes.h>
|
||||
#include <algorithm>
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
static uint32_t rprand_state[4] = {0};
|
||||
static uint64_t rprand_seed = 0;
|
||||
|
||||
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 z = (rprand_seed += 0x9e3779b97f4a7c15);
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
||||
return z ^ (z >> 31);
|
||||
}
|
||||
|
||||
namespace mrand
|
||||
{
|
||||
void setSeed(unsigned long long seed)
|
||||
{
|
||||
rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
|
||||
|
||||
// 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[0] = (uint32_t)(rprand_splitmix64() & 0xffffffff);
|
||||
rprand_state[1] = (uint32_t)((rprand_splitmix64() & 0xffffffff00000000) >> 32);
|
||||
rprand_state[2] = (uint32_t)(rprand_splitmix64() & 0xffffffff);
|
||||
rprand_state[3] = (uint32_t)((rprand_splitmix64() & 0xffffffff00000000) >> 32);
|
||||
}
|
||||
|
||||
int getValue(int min, int max)
|
||||
{
|
||||
int value = my_rprand_xoshiro(rprand_state) % (std::abs(max - min) + 1) + min;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
float getFloat()
|
||||
{
|
||||
return my_rprand_xoshiro(rprand_state) / 4294967295.0f;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user