From a0b1d289a744e186b16f29a949147ccb62925217 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Mon, 6 May 2024 07:47:14 +0200 Subject: [PATCH] add rand funcs --- src/misc.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/misc.c b/src/misc.c index fb707e2..99fc780 100644 --- a/src/misc.c +++ b/src/misc.c @@ -12,6 +12,9 @@ */ #include +#include + +#include #include "chessdefs.h" @@ -110,3 +113,40 @@ double clock_elapsed_sec(mclock_t *clock) { return (double) clock_elapsed_μs(clock) / (double) MICRO_IN_SEC; } + +static u64 rand_seed = 1ull; + +/** + * rand_init() - initialize random generator seed. + * @seed: u64, the random generator seed. + * + * No change is made is performed if If @seed is zero. By default, @seed is + * 1. + * This seed is used by rand64(). + */ +void rand_init(u64 seed) +{ + if (seed) + rand_seed = seed; +} + +/** + * rand64() - get a random number, xorshift method. + * + * Source: + * https://en.wikipedia.org/wiki/Xorshift#xorshift* + * We do not want true random numbers, like those offered by getrandom(2), as we + * need to be able to get predictable results. + * Note: For predictable results in MT, we should use separate seeds. + * + * @return: a 64 bits random number. + */ +u64 rand64(void) +{ + bug_on(rand_seed == 0ull); + + rand_seed ^= rand_seed >> 12; + rand_seed ^= rand_seed << 25; + rand_seed ^= rand_seed >> 27; + return rand_seed * 0x2545f4914f6cdd1dull; +}