aboutsummaryrefslogtreecommitdiff
path: root/km-rand-libc.c
diff options
context:
space:
mode:
Diffstat (limited to 'km-rand-libc.c')
-rw-r--r--km-rand-libc.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/km-rand-libc.c b/km-rand-libc.c
new file mode 100644
index 0000000..da1e4b0
--- /dev/null
+++ b/km-rand-libc.c
@@ -0,0 +1,56 @@
+#include <stdbool.h> // 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;
+}