diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2015-03-09 13:07:01 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2015-03-09 13:07:01 +0000 |
commit | 382d2d431d1f2080d9dae9baadaa00495d903c24 (patch) | |
tree | 552d9153288f9664a040a170beadc0875b8e994b | |
parent | d52c2a2c319ff866906f5072cc5730bf05e4bf35 (diff) | |
download | c-stuff-382d2d431d1f2080d9dae9baadaa00495d903c24.tar.gz c-stuff-382d2d431d1f2080d9dae9baadaa00495d903c24.tar.xz c-stuff-382d2d431d1f2080d9dae9baadaa00495d903c24.zip |
Minor modifications and new file endianness.c
-rw-r--r-- | endianness.c | 23 | ||||
-rw-r--r-- | randtest.c | 47 |
2 files changed, 52 insertions, 18 deletions
diff --git a/endianness.c b/endianness.c new file mode 100644 index 0000000..c893be2 --- /dev/null +++ b/endianness.c @@ -0,0 +1,23 @@ +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +int main(void) +{ + bool big_endian; + + union { + uint32_t i; + char c[4]; + } test = {0x01020304}; + + big_endian = test.c[0] == 1; + + if (big_endian) + printf("%s\n", "Big Endian"); + else + printf("%s\n", "Small Endian"); + + return EXIT_SUCCESS; +} @@ -8,28 +8,39 @@ 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 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 + 1); } 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]++; + 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 < TO - FROM + 1; i++) - printf("%d: %d\n", i, amounts[i]); + for (int i = 0; i < 10; i++) + printf("%d: %lu\n", i, counter[i]); - return 0; + return EXIT_SUCCESS; } |