Skip to content

Instantly share code, notes, and snippets.

@amano41
Created October 24, 2018 15:21
Show Gist options
  • Save amano41/135cd510135ee39850c7c8a409ff8c60 to your computer and use it in GitHub Desktop.
Save amano41/135cd510135ee39850c7c8a409ff8c60 to your computer and use it in GitHub Desktop.
Xorshift and xorshift+ implementation in Java
public class Xorshift {
private long x = 123456789;
private long y = 362436069;
private long z = 521288629;
private long w = 88675123;
public long random() {
long t = x ^ ((x << 11) & 0xFFFFFFFFL); // 32bit
x = y;
y = z;
z = w;
w ^= (w >>> 19) ^ t ^ (t >>> 8);
return w;
}
public static void main(String[] args) {
Xorshift rng = new Xorshift();
for (int i = 0; i < 10; i++) {
System.out.println(rng.random());
}
}
}
public class XorshiftPlus {
private long s0 = 1;
private long s1 = 2;
public long random() {
long x = s0;
long y = s1;
x ^= x << 23;
x ^= (x >>> 17) ^ y ^ (y >>> 26);
s0 = y;
s1 = x;
return s0 + s1;
}
public static void main(String[] args) {
XorshiftPlus rng = new XorshiftPlus();
for (int i = 0; i < 10; i++) {
System.out.println(rng.random());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment