rework code to be more accurate and platform-independent
This commit is contained in:
91
src/main.c
91
src/main.c
@@ -1,60 +1,61 @@
|
||||
#include <features.h>
|
||||
#if _POSIX_C_SOURCE >= 199309L
|
||||
# include <bits/time.h>
|
||||
# include <errno.h>
|
||||
# include <limits.h>
|
||||
# include <stdarg.h>
|
||||
# include <stdint.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdnoreturn.h>
|
||||
# include <time.h>
|
||||
#include <bits/time.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
noreturn static inline void error(char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
exit(1);
|
||||
#define error(ret, s, ...) \
|
||||
do { \
|
||||
fprintf(stderr, (s), ##__VA_ARGS__); \
|
||||
exit(ret); \
|
||||
} while (0)
|
||||
|
||||
typedef unsigned long long ull;
|
||||
|
||||
/* acquires the random data */
|
||||
long rdat(void) {
|
||||
return random();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// set the seed to the number of seconds xor with nanoseconds
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds
|
||||
}
|
||||
// get the current time to set as the seed
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
srandom(ts.tv_nsec);
|
||||
|
||||
// just print whether it's heads or tails
|
||||
if (argc == 1) {
|
||||
printf("%s\n", (rand() & 1) ? "heads" : "tails");
|
||||
return 0;
|
||||
}
|
||||
// return the result if no input
|
||||
if (argc <= 1) return printf("%s\n", (random() & 1) ? "heads" : "tails");
|
||||
|
||||
// if we have input; perform coin flips for those times
|
||||
for (uint32_t i = 1; i < (unsigned)argc; ++i) {
|
||||
int n = 0; // contains the random data
|
||||
uint64_t headsc = 0; // amount of heads
|
||||
uint64_t tailsc = 0; // amount of tails
|
||||
|
||||
// get the integer from the string, return 1 if an error occurred
|
||||
// loop through arguments
|
||||
for (unsigned i = 1; i < (unsigned)argc; ++i) {
|
||||
// parse the argument
|
||||
errno = 0;
|
||||
long const c = strtol(argv[i], NULL, 10);
|
||||
if (errno == EINVAL) error("syntax error for string: %s\n", argv[i]);
|
||||
if (c < 1 || c > (LONG_MAX - 1)) error("count may only be in between 1 and %l. Received %l\n", LONG_MAX, c);
|
||||
ull c = strtoull(argv[i], NULL, 10);
|
||||
if (errno != 0) error(errno, "parse error for string: '%s'\n", argv[i]);
|
||||
if (c < 1 || c > (ULLONG_MAX - 1)) error(1, "count may only be in between 1 and %llu. Received %llu\n", ULLONG_MAX - 1, c);
|
||||
|
||||
// perform for the input count
|
||||
for (long j = 0; j < c; ++j) {
|
||||
if (n == 0) n = rand(); // get a new random number if the random data ran out
|
||||
(void)((n & 1) ? headsc++ : tailsc++); // increment the correct counter
|
||||
n >>= 1; // shift the random integer right by 1
|
||||
ull headsc = 0; // amount of heads
|
||||
ull tailsc = 0; // amount of tails
|
||||
|
||||
|
||||
for (ull j = 0; j < c; ++j) {
|
||||
ull n = rdat();
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 8
|
||||
# pragma GCC unroll 31
|
||||
#elif defined(__clang__)
|
||||
# pragma unroll 31
|
||||
#endif
|
||||
for (ull b = 1; b <= INT32_MAX; b <<= 1)
|
||||
(void)((n & b) ? headsc++ : tailsc++); // increment the correct counter
|
||||
}
|
||||
|
||||
// print the results of this cycle
|
||||
printf("results:\n heads: %lu\n tails: %lu\n", headsc, tailsc);
|
||||
printf("results:\n heads: %llu\n tails: %llu\n", headsc, tailsc);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
# error "platform unsupported"
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user