From bd347df5d57cc18aa22bd97fb834e99de1c962e5 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Sun, 7 Nov 2021 01:29:07 +0800 Subject: [PATCH] driver/random: linear congruential --- driver/random/random.c | 15 +++++++++++++++ driver/random/random.h | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 driver/random/random.c create mode 100644 driver/random/random.h diff --git a/driver/random/random.c b/driver/random/random.c new file mode 100644 index 0000000..8be38c7 --- /dev/null +++ b/driver/random/random.c @@ -0,0 +1,15 @@ + +#include "random.h" + +// glibc defaults +static const uint32_t a = 1103515245, c = 12345, m = 2147483648; +static uint32_t v = 1; + + +void random_Seed(uint32_t seed) { + v = seed; +} + +uint32_t random_Rand() { + return v = (uint32_t)(((uint64_t)v * a + c) % m); +} diff --git a/driver/random/random.h b/driver/random/random.h new file mode 100644 index 0000000..0e99339 --- /dev/null +++ b/driver/random/random.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +void random_Seed(uint32_t seed); + +uint32_t random_Rand(); + + +#ifdef __cplusplus +} +#endif