-
Notifications
You must be signed in to change notification settings - Fork 2
/
rngod.c
48 lines (37 loc) · 862 Bytes
/
rngod.c
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
#include <stdint.h>
#include <talloc.h>
#include "rngod.h"
#include "rng-private.h"
int
rng_free(struct rngod *rng){
return talloc_free(rng);
}
uint32_t
default_dx(struct rngod *rng, uint32_t x){
if (x < 1) x = 1;
return rng->rand(rng) % x + 1;
}
uint32_t
default_ndx(struct rngod *rng, uint32_t n, uint32_t x){
uint32_t i;
uint32_t total;
if (n == 0) return 0;
if (x == 0) return 0;
for (total = 0, i = 0 ; i < n ; i ++){
total += rng->dx(rng, x);
}
return total;
}
uint64_t
default_range(struct rngod *rng, uint64_t min, uint64_t max){
int range = max - min + 1;
return min + rng->dx(rng, range) - 1;
}
int
default_init(struct rngod *rng){
if (!rng) return -1;
rng->dx = default_dx;
rng->ndx = default_ndx;
rng->range = default_range;
return 0;
}