prng

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

commit ddc1030edf4395ad4b2894794146c4bb344b630c
parent 77a38276e4f0286370fe0a8211a32af7a7761633
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sat,  4 Apr 2020 19:31:39 -0500

Revert "created general interface for prng, converted acorn"

This reverts commit 77a38276e4f0286370fe0a8211a32af7a7761633.

general interface too hard, resetting to different interfaces

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

diff --git a/acorn.c b/acorn.c @@ -1,33 +0,0 @@ -#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/README b/acorn/README diff --git a/acorn/acorn.c b/acorn/acorn.c @@ -0,0 +1,27 @@ +#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 @@ -0,0 +1,13 @@ +#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 @@ -1,9 +0,0 @@ -#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);