prng

some implementations of pseudo-random number generators, wip
git clone git://nihaljere.xyz/prng
Log | Files | Refs

commit 77a38276e4f0286370fe0a8211a32af7a7761633
parent 6bcc766b4e3d6670aa8dd0af1ec96751642188b8
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sat,  4 Apr 2020 17:07:31 -0500

created general interface for prng, converted acorn

Diffstat:
Racorn/README -> README | 0
Aacorn.c | 33+++++++++++++++++++++++++++++++++
Dacorn/acorn.c | 27---------------------------
Dacorn/acorn.h | 13-------------
Aprng.h | 9+++++++++
5 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/acorn/README b/README diff --git a/acorn.c b/acorn.c @@ -0,0 +1,33 @@ +#include "prng.h" + +#define STATE_VECTOR_LENGTH 624 +struct { + unsigned long state[STATE_VECTOR_LENGTH]; + int index; +} PRNGState; + +unsigned long MODULUS = 1 << 30; + +PRNGState +prng_seed(unsigned long seed) +{ + PRNGState prng; + prng.state[0] = seed & 0xffffffff; + for (prng.index=1; prng.index<STATE_VECTOR_LENGTH; ++prng.index) + prng.state[prng.index] = (69069 * prng.state[prng.index-1]) % MODULUS; + return prng; +} +unsigned long +prng_long(PRNGState* prng) +{ + for (prng->index = 1; prng->index < STATE_VECTOR_LENGTH; ++prng->index) { + prng->state[prng->index] += prng->state[prng->index-1]; + prng->state[prng->index] %= MODULUS; + } + + return prng->state[STATE_VECTOR_LENGTH - 1]; +} +double prng_get_double(PRNGState* prng) { + unsigned long val = prng_long(prng); + return (double)val / MODULUS; +} diff --git a/acorn/acorn.c b/acorn/acorn.c @@ -1,27 +0,0 @@ -#include "acorn.h" - -unsigned long STATE_VECTOR_M = 1 << 30; - -AcornState -acorn_seed(unsigned long seed) -{ - AcornState acorn; - acorn.state[0] = seed & 0xffffffff; - for (acorn.index=1; acorn.index<STATE_VECTOR_LENGTH; ++acorn.index) - acorn.state[acorn.index] = (69069 * acorn.state[acorn.index-1]) % STATE_VECTOR_M; - return acorn; -} -unsigned long -acorn_get_long(AcornState* acorn) -{ - for (acorn->index = 1; acorn->index < STATE_VECTOR_LENGTH; ++acorn->index) { - acorn->state[acorn->index] += acorn->state[acorn->index-1]; - acorn->state[acorn->index] %= STATE_VECTOR_M; - } - - return acorn->state[STATE_VECTOR_LENGTH - 1]; -} -double acorn_get_double(AcornState* acorn) { - unsigned long val = acorn_get_long(acorn); - return (double)val / STATE_VECTOR_M; -} diff --git a/acorn/acorn.h b/acorn/acorn.h @@ -1,13 +0,0 @@ -#pragma once - -#define STATE_VECTOR_LENGTH 624 -extern unsigned long STATE_VECTOR_M; - -typedef struct { - unsigned long state[STATE_VECTOR_LENGTH]; - int index; -} AcornState; - -AcornState acorn_seed(unsigned long seed); -unsigned long acorn_get_long(AcornState* acorn); -double acorn_get_double(AcornState* acorn); diff --git a/prng.h b/prng.h @@ -0,0 +1,9 @@ +#pragma once + +extern unsigned long MODULUS; + +typedef struct PRNGState; + +PRNGState prng_seed(unsigned long seed); +unsigned long prng_long(PRNGState* acorn); +double prng_double(PRNGState* acorn);