-
Notifications
You must be signed in to change notification settings - Fork 0
/
randtest.c
55 lines (44 loc) · 1.17 KB
/
randtest.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
/*
* Copyright (C) 2015 Tomasz Kramkowski <tk@the-tk.com>
*
* This program is free software. It is licensed under version 3 of the
* GNU General Public License.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FROM 0
#define TO 9
int8_t randrange(int8_t from, int8_t to)
{
int base_random = rand();
if (RAND_MAX == base_random) return randrange(from, to);
int range = to - from,
remainder = RAND_MAX % range,
bucket = RAND_MAX / range;
if (base_random < RAND_MAX - remainder) {
return from + base_random/bucket;
} else {
return randrange(from, to);
}
}
static unsigned rand_to_max(unsigned max)
{
double random;
while ((random = rand()) == (double)RAND_MAX);
return random / (double)RAND_MAX * (double)max;
}
int main(int argc, char **argv)
{
srand(time(NULL));
uint64_t counter[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < 1000000; i++)
counter[rand_to_max(9)]++;
for (int i = 0; i < 10; i++)
printf("%d: %lu\n", i, counter[i]);
return EXIT_SUCCESS;
}