-
Notifications
You must be signed in to change notification settings - Fork 0
/
random32.c
102 lines (77 loc) · 1.43 KB
/
random32.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
bool get_bytes(int n, void *bytes_out)
{
FILE *fp;
size_t nread;
bool b;
fp = fopen("/dev/urandom", "r");
if(!fp) {
b = false;
goto out;
}
nread = fread(bytes_out, 1, n, fp);
if(nread != n) {
b = false;
goto out;
}
b = true;
out:
return b;
}
uint32_t get_random_u32(void)
{
bool b;
uint32_t val;
b = get_bytes(4, &val);
if(!b) {
fprintf(stderr, "Error: Failed to get random bytes\n");
exit(1);
}
return val;
}
static uint32_t string_to_u32(const char *s)
{
int n;
uint32_t v;
n = sscanf(s, "%"PRIu32, &v);
if(n != 1) {
fprintf(stderr, "Error: Invalid number: %s\n", s);
exit(1);
}
return v;
}
uint32_t get_random_u32_range(uint32_t range_min, uint32_t range_max)
{
uint32_t range_size;
uint32_t val;
range_size = range_max - range_min + 1;
val = range_min + (get_random_u32() % range_size);
return val;
}
int main(int argc, char **argv)
{
uint32_t val;
if(argc == 1) {
val = get_random_u32();
} else {
uint32_t range_min;
uint32_t range_max;
if(argc == 2) {
range_min = 0;
range_max = string_to_u32(argv[1]);
} else if(argc == 3) {
range_min = string_to_u32(argv[1]);
range_max = string_to_u32(argv[2]);
} else {
fprintf(stderr, "Error: Invalid args\n");
exit(1);
}
val = get_random_u32_range(range_min, range_max);
}
printf("%u\n", val);
return 0;
}