C: Fill an array of unsigned integers with random data using getentropy()
Often I need to generate random unsigned integers for seeding PRNGs. The best way is using the getentropy() system function to read OS level randomness into your array.
#include <sys/random.h>
// Read from system random to fill up data structure
int8_t fill_urandom(void *buf, size_t bytes) {
int8_t ok = getentropy(buf, bytes);
return (ok == 0);
}
Declare your array of integers, and then pass it as a pointer to this function to fill with random bytes from your OS. This will get you a bunch of random integers you can use for seeding PRNGs.
uint64_t seed[4];
fill_urandom(seed, sizeof(seed));
Tags:



