dimanche 25 juin 2017

Can XorShift return zero?

I've been reading about the XorShift PRNG especially the paper here

A guy here states that

The number lies in the range [1, 2**64). Note that it will NEVER be 0.

Looking at the code that makes sense:

uint64_t x;
uint64_t next(void) {
   x ^= x >> 12; // a
   x ^= x << 25; // b
   x ^= x >> 27; // c
   return x * UINT64_C(2685821657736338717);
}

If x would be zero than every next number would be zero too. But wouldn't that make it less useful? The usual use-pattern would be something like min + rand() % (max - min) or converting the 64 bits to 32 bits if you only need an int. But if 0 is never returned than that might be a serious problem. Also the bits are not 0 or 1 with the same probability as obviously 0 is missing so zeroes or slightly less likely. I even can't find any mention of that on Wikipedia so am I missing something?

So what is a good/appropriate way to generate random, equally distributed numbers from XorShift64* in a given range?




Aucun commentaire:

Enregistrer un commentaire