I need a fast random number generator that allows me to randomly access numbers at various positions of the random number sequence. I chose Xorshift, because it's fast and easy to implement.
To get a specific random number from the sequence, I implemented the following method (mPos
saves the position of the next random number):
void XorshiftRandomGenerator::skipTo(unsigned int pos)
{
// Reset if we passed the position
if (mPos>pos)
reset();
// Generate random numbers until we're done
while (mPos<pos)
random();
}
A subsequent random()
will return the desired number, but this method is very expensive. Is there a way to skip a large amount of random numbers with Xorshift, without calculating every random number in between?
As an alternative I could go with another random number generator. Could you suggest one that allows fast skipping ahead?