For non-cryptographic purposes (which use a HWRNG instead), I implemented into a bare metal program a Xorshift* RNG (Taken from [1], see below for implementation).
The RNG is currently seeded with the cycle counter and I'd like to XOR in a second cycle counter value once some hardware was initialized that takes a varying amount of time.
I am wondering whether it's ok to:
prng_srand(cycle_counter());
initialize_hardware_and_bind_drivers();
prng_srand(cycle_counter());
Or if I need to do the following instead:
u64 initial = cycle_counter();
prng_srand(initial);
initialize_hardware_and_bind_drivers();
prng_srand(cycle_counter() - initial);
To make the two seedings of the PRNG independent of each other and not impact quality negatively?
Here's the implementation:
static u64 prng_state = 1;
static u32 prng_rand(void)
{
u64 x = prng_state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
prng_state = x;
return (x * 0x2545F4914F6CDD1DULL) >> 32;
}
void prng_srand(u64 entropy)
{
prng_state ^= entropy;
/* Ensure prng_state is never zero */
prng_state += !prng_state;
prng_rand()
}