From 344960f7470d7b6e5cb0c0b1d7e751d47063a98c Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Sat, 7 May 2022 08:09:10 -0700 Subject: Add random library. --- random/include/random/mt19937-64.h | 40 ++++++++++++++++++++++++++++++++++++++ random/include/random/random.h | 3 +++ 2 files changed, 43 insertions(+) create mode 100644 random/include/random/mt19937-64.h create mode 100644 random/include/random/random.h (limited to 'random/include') diff --git a/random/include/random/mt19937-64.h b/random/include/random/mt19937-64.h new file mode 100644 index 0000000..bd1125f --- /dev/null +++ b/random/include/random/mt19937-64.h @@ -0,0 +1,40 @@ +#pragma once + +#include + +#define MT19937_64_NN 312 + +typedef struct mt19937_64 { + uint64_t mt[MT19937_64_NN]; // The array for the state vector. + int mti; // mti==NN+1 means mt[NN] is not initialized. +} mt19937_64; + +/// Creates a default-initialized 64-bit Mersenne Twister PRNG. +mt19937_64 mt19937_64_make(); + +/// Initializes the PRNG with a seed. +void mt19937_64_init(mt19937_64* rng, uint64_t seed); + +/// Initializes the PRNG with an array of values. +/// |init_key| is the array for initializing keys. +/// |key_length| is its length. +void mt19937_64_init_by_array64( + mt19937_64* rng, uint64_t init_key[], uint64_t key_length); + +/// Generates a random number in the [0, 2^64-1]-interval. +uint64_t mt19937_64_gen64u(mt19937_64* rng); + +/// Generates a random number in the [0, 2^63-1]-interval. +int64_t mt19937_64_gen64i(mt19937_64* rng); + +/// Generates a random number in the [0,1]-real-interval. +double mt19937_64_gen_real1(mt19937_64* rng); + +/// Generates a random number in the [0,1)-real-interval. +double mt19937_64_gen_real2(mt19937_64* rng); + +/// Generates a random number in the (0,1)-real-interval. +double mt19937_64_gen_real3(mt19937_64* rng); + +/// Generates a random number in the (-1,+1)-real-interval. +double mt19937_64_gen_real4(mt19937_64* rng); diff --git a/random/include/random/random.h b/random/include/random/random.h new file mode 100644 index 0000000..5499f62 --- /dev/null +++ b/random/include/random/random.h @@ -0,0 +1,3 @@ +#pragma once + +#include -- cgit v1.2.3