Is there an alternative to using time to seed a random number generation?

The rdtsc instruction is a pretty reliable (and random) seed.

In Windows it’s accessible via the __rdtsc() intrinsic.

In GNU C, it’s accessible via:

unsigned long long rdtsc(){
    unsigned int lo,hi;
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return ((unsigned long long)hi << 32) | lo;
}

The instruction measures the total pseudo-cycles since the processor was powered on. Given the high frequency of today’s machines, it’s extremely unlikely that two processors will return the same value even if they booted at the same time and are clocked at the same speed.

Leave a Comment