-
Notifications
You must be signed in to change notification settings - Fork 2
/
constant.c
70 lines (56 loc) · 1.48 KB
/
constant.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <talloc.h>
#include "rngod.h"
#include "rng-private.h"
#include "constant.h"
struct rng_constant {
struct rngod rng;
uint64_t value;
};
static uint64_t constant_rand(struct rngod *rng);
static uint32_t constant_dx(struct rngod *rng, uint32_t x);
static uint64_t constant_range(struct rngod *rng, uint64_t min, uint64_t max);
struct rngod *
rngod_constant_add_default(void){
return rngod_constant_add(7);
}
struct rngod *
rngod_constant_add(uint64_t value){
struct rng_constant *rngc;
rngc = talloc(NULL, struct rng_constant);
default_init(&rngc->rng);
rngc->value = value;
rngc->rng.rand = constant_rand;
rngc->rng.dx = constant_dx;
rngc->rng.range = constant_range;
return (struct rngod *)rngc;
}
int
rngod_constant_constant_set(struct rngod *rng, uint64_t seed) {
struct rng_constant *rngc = talloc_get_type(rng, struct rng_constant);
if (!rngc) return -1;
rngc->value = seed;
return 0;
}
static uint64_t
constant_rand(struct rngod *rng) {
struct rng_constant *rngc = talloc_get_type(rng, struct rng_constant);
return rngc->value;
}
static uint32_t
constant_dx(struct rngod *rng, uint32_t x) {
return constant_range(rng, 1, x);
}
static uint64_t
constant_range(struct rngod *rng, uint64_t min, uint64_t max) {
struct rng_constant *rngc = talloc_get_type(rng, struct rng_constant);
if (rngc->value > max)
return max;
else if (rngc->value < min)
return min;
else
return rngc->value;
}