aboutsummaryrefslogtreecommitdiff
path: root/src/km-rand-libc.c
blob: da1e4b03451dc541981ab62a34a6d0693998b3d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}