From 4e75b40d8a96c5d51490ddea43566fe8e6d4168c Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 9 Apr 2022 02:28:05 +0000 Subject: Prepare for 2.12.0.0. librandom revamp. Signed-off-by: Laurent Bercot --- src/librandom/random_uint32_from.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/librandom/random_uint32_from.c (limited to 'src/librandom/random_uint32_from.c') diff --git a/src/librandom/random_uint32_from.c b/src/librandom/random_uint32_from.c new file mode 100644 index 0000000..621c438 --- /dev/null +++ b/src/librandom/random_uint32_from.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include +#include +#include + +uint32_t random_uint32_from (uint32_t n, randomgen_func_ref f) +{ + uint32_t min, x ; + if (n < 2) return 0 ; + min = -n % n ; + for (;;) + { + char tmp[4] ; + (*f)(tmp, 4) ; + uint32_unpack_big(tmp, &x) ; + if (x >= min) break ; + } + return x % n ; +} -- cgit v1.3.1