From f557d1f49a2914c6084dd18efc783395228d8ce0 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 5 Feb 2019 00:22:15 -0500 Subject: mv *.[hc] src/ --- src/km-rand-libc.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/km-rand-libc.c (limited to 'src/km-rand-libc.c') diff --git a/src/km-rand-libc.c b/src/km-rand-libc.c new file mode 100644 index 0000000..da1e4b0 --- /dev/null +++ b/src/km-rand-libc.c @@ -0,0 +1,56 @@ +#include // bool +#include "util.h" +#include "km.h" + +// libc random source get_floats +static bool +on_get_floats( + km_rand_t * const rs, + const size_t num_vals, + float * const vals +) { + UNUSED(rs); + + // generate results + for (size_t i = 0; i < num_vals; i++) { + vals[i] = 1.0 * rand() / RAND_MAX; + } + + // return success + return true; +} + +// fill sizes callback for system random source +static bool +on_get_sizes( + km_rand_t * const rs, + const size_t num_vals, + size_t * const vals +) { + UNUSED(rs); + + // generate results + for (size_t i = 0; i < num_vals; i++) { + vals[i] = rand(); + } + + // return success + return true; +} + +// system random source callbacks +static const km_rand_cbs_t +LIBC_RAND_CBS = { + .get_floats = on_get_floats, + .get_sizes = on_get_sizes, + .fini = NULL, +}; + +// init system random source (uses system rand()) +void +km_rand_init_libc( + km_rand_t * const rs +) { + rs->cbs = &LIBC_RAND_CBS; + rs->data = NULL; +} -- cgit v1.2.3