improved random seed to be less predictable

This commit is contained in:
unset
2025-01-30 16:09:23 +01:00
parent e55fd0f915
commit c8dce9dc0e
2 changed files with 28 additions and 12 deletions

View File

@@ -1,18 +1,12 @@
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(void) {
srand(time(NULL)); // set the random seed to the system time
char* result = NULL;
if (rand() & (~1) != 0) {
result = "heads";
}
else {
result = "tails";
}
printf("%s\n", result);
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
srand((uint32_t)(ts.tv_nsec ^ ts.tv_sec)); // combine seconds and nanoseconds
printf("%s\n", (rand() & 1) ? "heads" : "tails");
return 0;
}