aboutsummaryrefslogtreecommitdiffstats
path: root/src/librandom/random_uint32_from.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/librandom/random_uint32_from.c')
-rw-r--r--src/librandom/random_uint32_from.c20
1 files changed, 20 insertions, 0 deletions
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 <skalibs/uint32.h>
+#include <skalibs/functypes.h>
+#include <skalibs/random.h>
+
+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 ;
+}