Create RNG module
This commit is contained in:
32
rng.c
Normal file
32
rng.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user