Create RNG module

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent c1f339484b
commit f992dd3d39
5 changed files with 50 additions and 13 deletions

32
rng.c Normal file
View File

@@ -0,0 +1,32 @@
#include "rng.h"
#include <assert.h>
#include <sys/time.h>
static uint32_t state;
void rng_init()
{
struct timeval tv;
const int res = gettimeofday(&tv, nullptr);
assert(res == 0);
state = tv.tv_usec;
}
uint32_t rng_uint32()
{
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
return state;
}
float rng_canon()
{
return (float)rng_uint32() / UINT32_MAX;
}
float rng_plusminus()
{
return 2.0 * rng_canon() - 1.0;
}