ddnet/src/game/prng.cpp
Chairn a69dc599a9 Huge variable naming format
Fix pointer and pointer array variable naming

Huge renaming to match our rules

Used regex: (?!(return|delete)\b)\b\w+ (m_|ms_|g_|gs_|s_)[^a]\w+\[
            (?!(return|delete)\b)\b\w+ (?!(m_|ms_|g_|gs_|s_))[^a]\w+\[

Further format static variables

Format almost all pointer names accordingly

Used regex: (?!(return)\b)\b\w+
\*(?!(m_p|p|s_p|m_ap|s_ap|g_p|g_ap|ap|gs_ap|ms_ap|gs_p|ms_p))\w+\b[^:\(p]

clang-format

Fix CI fail

Fix misnamed non pointer as pointer and non array as array

Used regex: (?!(return|delete)\b)\b\w+ (m_|ms_|g_|gs_|s_)p\w+\b
            (?!return\b)\b\w+ (ms_|m_|g_|gs_|s_)a\w+\b[^\[]

clang-format

Revert to SCREAMING_SNAKE_CASE and reinstate dead code
2022-07-08 18:01:29 +02:00

55 lines
1.2 KiB
C++

#include "prng.h"
#include <base/system.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_t aSeed[2])
{
m_Seeded = true;
str_format(m_aDescription, sizeof(m_aDescription), "%s:%08x%08x:%08x%08x", NAME,
(unsigned)(aSeed[0] >> 32), (unsigned)aSeed[0],
(unsigned)(aSeed[1] >> 32), (unsigned)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_t x = m_State;
unsigned int Count = x >> 59;
static const uint64_t s_Multiplier = 6364136223846793005u;
m_State = x * s_Multiplier + m_Increment;
x ^= x >> 18;
return RotateRight32(x >> 27, Count);
}