Get seed from /dev/urandom instead of using time(2)

This commit is contained in:
Camden Dixie O'Brien 2022-11-21 19:22:02 +00:00
parent 512580ab27
commit 5ae7a18227

19
main.c
View File

@ -18,13 +18,28 @@
#include "sud.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static uint32_t getseed(void)
{
FILE *urandom = fopen("/dev/urandom", "rb");
if (urandom == NULL)
fprintf(stderr, "Failed to open /dev/urandom\n");
uint32_t seed = 0;
for (unsigned i = 0; i < 4; ++i)
seed = seed << 8 | fgetc(urandom);
fclose(urandom);
return seed;
}
int main(void)
{
unsigned seed = time(NULL);
uint32_t seed = getseed();
printf("Seed: %u\n\n", seed);
srand(seed);