mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-14 03:58:18 +00:00
c9c7f947b5
This came from a long discussion comparing PCG-* against xoroshiro*. Do not generate integers without bias because it doesn't affect us very much and it is easier to reimplement with modulo.
52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#include "prng.h"
|
|
|
|
// From https://en.wikipedia.org/w/index.php?title=Permuted_congruential_generator&oldid=901497400#Example_code.
|
|
//
|
|
// > The generator recommended for most users is PCG-XSH-RR with 64-bit state
|
|
// > and 32-bit output.
|
|
|
|
#define NAME "pcg-xsh-rr"
|
|
|
|
CPrng::CPrng() :
|
|
m_Seeded(false)
|
|
{
|
|
}
|
|
|
|
|
|
const char *CPrng::Description() const
|
|
{
|
|
if(!m_Seeded)
|
|
{
|
|
return NAME ":unseeded";
|
|
}
|
|
return m_aDescription;
|
|
}
|
|
|
|
static unsigned int RotateRight32(unsigned int x, int Shift)
|
|
{
|
|
return (x >> Shift) | (x << (-Shift & 31));
|
|
}
|
|
|
|
void CPrng::Seed(uint64 aSeed[2])
|
|
{
|
|
m_Seeded = true;
|
|
str_format(m_aDescription, sizeof(m_aDescription), "%s:%016llx:%016llx", NAME, aSeed[0], aSeed[1]);
|
|
|
|
m_Increment = (aSeed[1] << 1) | 1;
|
|
m_State = aSeed[0] + m_Increment;
|
|
RandomBits();
|
|
}
|
|
|
|
unsigned int CPrng::RandomBits()
|
|
{
|
|
dbg_assert(m_Seeded, "prng needs to be seeded before it can generate random numbers");
|
|
|
|
uint64 x = m_State;
|
|
unsigned int Count = x >> 59;
|
|
|
|
static const uint64 MULTIPLIER = 6364136223846793005u;
|
|
m_State = x * MULTIPLIER + m_Increment;
|
|
x ^= x >> 18;
|
|
return RotateRight32(x >> 27, Count);
|
|
}
|