diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-02-03 05:03:07 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-02-03 05:03:07 -0500 |
commit | e5f97aafca24667ed6780a92d9bd451c66454b44 (patch) | |
tree | 385e60e48d7a736f1611b83f9ce81ea2c97b5665 /km-rand.c | |
parent | b3d3cebbc3dae2c975b9bb0f0c77f5dc845d7fb8 (diff) | |
download | kmeans-e5f97aafca24667ed6780a92d9bd451c66454b44.tar.bz2 kmeans-e5f97aafca24667ed6780a92d9bd451c66454b44.zip |
s/rand_src/rand/, mv km-rand{-src,}.c
Diffstat (limited to 'km-rand.c')
-rw-r--r-- | km-rand.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/km-rand.c b/km-rand.c new file mode 100644 index 0000000..fcc6a74 --- /dev/null +++ b/km-rand.c @@ -0,0 +1,57 @@ +#include <stdbool.h> +#include "util.h" +#include "km.h" + +// fill buffer with N random floats +bool +km_rand_fill( + km_rand_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_fini( + km_rand_t * const rs +) { + if (rs->cbs->fini) { + rs->cbs->fini(rs); + } +} + +// fill callback for system random source +static bool +system_on_fill( + km_rand_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_cbs_t +SYSTEM_CBS = { + .fill = system_on_fill, + .fini = NULL, +}; + +// init system random source (uses system rand()) +void +km_rand_init_system( + km_rand_t * const rs +) { + rs->cbs = &SYSTEM_CBS; + rs->data = NULL; +} |