Using the Xorshift random number generator... I already have the generator, but I haven't been able to modify it to give a number between 0 and an upper limit (like the nextInt()
method in the Java Random class).
long seed = System.nanoTime();
int next(int nbits) {
long x = seed;
x ^= (x << 21);
x ^= (x >>> 35);
x ^= (x << 4);
seed = x;
x &= ((1L << nbits) -1);
return (int) x;
}
Any ideas?
you can see what java does with random class
((int)x) % (upper_limit + 1)
? (That's a sincere question; I don't know enough about the properties of Xorshift -- specifically, about how much entropy it has in the lower bits -- to know if that's a good idea. - ruakh 2012-04-04 19:23