aboutsummaryrefslogtreecommitdiff
path: root/km-rand-src.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-02-03 04:54:10 -0500
committerPaul Duncan <pabs@pablotron.org>2019-02-03 04:54:10 -0500
commit1e3dc2401263a436a32377e207352ffbe6984e8f (patch)
treefc34de6b8d7a456d861d7534360073ad10d8c620 /km-rand-src.c
parenta3792d8769d2dc8ee0abae758c6fae3a35b5dfbc (diff)
downloadkmeans-1e3dc2401263a436a32377e207352ffbe6984e8f.tar.bz2
kmeans-1e3dc2401263a436a32377e207352ffbe6984e8f.zip
add outstanding changes
Diffstat (limited to 'km-rand-src.c')
-rw-r--r--km-rand-src.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/km-rand-src.c b/km-rand-src.c
new file mode 100644
index 0000000..0ddc1bb
--- /dev/null
+++ b/km-rand-src.c
@@ -0,0 +1,57 @@
+#include <stdbool.h>
+#include "util.h"
+#include "km.h"
+
+// fill buffer with N random floats
+bool
+km_rand_src_fill(
+ km_rand_src_t * const rs,
+ const size_t num_floats,
+ float * const floats
+) {
+ return rs->cbs->fill(rs, num_floats, floats);
+}
+
+// finalize random source
+void
+km_rand_src_fini(
+ km_rand_src_t * const rs
+) {
+ if (rs->cbs->fini) {
+ rs->cbs->fini(rs);
+ }
+}
+
+// fill callback for system random source
+static bool
+rand_src_system_on_fill(
+ km_rand_src_t * const rs,
+ const size_t num_floats,
+ float * const floats
+) {
+ UNUSED(rs);
+
+ // generate random cluster centers
+ for (size_t i = 0; i < num_floats; i++) {
+ floats[i] = 1.0 * rand() / RAND_MAX;
+ }
+
+ // return success
+ return true;
+}
+
+// system random source callbacks
+static const km_rand_src_cbs_t
+RAND_SRC_SYSTEM_CBS = {
+ .fill = rand_src_system_on_fill,
+ .fini = NULL,
+};
+
+// init system random source (uses system rand())
+void
+km_rand_src_system_init(
+ km_rand_src_t * const rs
+) {
+ rs->cbs = &RAND_SRC_SYSTEM_CBS;
+ rs->data = NULL;
+}