improved random seed to be less predictable
This commit is contained in:
18
src/main.c
18
src/main.c
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user