From 0a23128c376540cd139dde19a1f09ae40617e670 Mon Sep 17 00:00:00 2001 From: EliteTK Date: Sat, 6 Sep 2014 18:33:33 +0100 Subject: Latest 'stuff' --- randtest.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 randtest.c (limited to 'randtest.c') diff --git a/randtest.c b/randtest.c new file mode 100644 index 0000000..06a71de --- /dev/null +++ b/randtest.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +#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); + } +} + +int main(int argc, char **argv) +{ + srand(time(NULL)); + int r; + uint16_t *amounts = calloc(sizeof(uint16_t), (TO - FROM + 1)); + for (int i = 0; i < 1000; i++) + amounts[randrange(FROM, TO) - FROM]++; + + for (int i = 0; i < TO - FROM + 1; i++) + printf("%d: %d\n", i, amounts[i]); + + return 0; +} -- cgit v1.2.3-54-g00ecf