I would like to implement XorShift PRNG in Java, Python and JavaScript. Different implementations must generate exact sequences defined by the same seed. So far, I have not been able to do this.
My implementation in Java
have the following implementation of XORShift PRNG in Java (where xis this field long):
public long randomLong() {
x ^= (x << 21);
x ^= (x >>> 35);
x ^= (x << 4);
return x;
}
If I run xat 1, the first four calls randomLong()will generate:
35651601
1130297953386881
-9204155794254196429
144132848981442561
My Python implementation
I have tried both with and without numpy. Below is the version using numpy.
def randomLong(self):
self.x ^= np.left_shift(self.x, 21)
self.x ^= np.right_shift(self.x, 35)
self.x ^= np.left_shift(self.x, 4)
return self.x
With the same seed, the Python function will generate:
35651601
1130297953386881
-9204155787274874573
143006948545953793
My JavaScript implementation
I have not tried to use it yet, because the JavaScript number type seems to double based on IEEE 754, which opens up a different can of worms.
I think the reason
Java Python . Java 32 64- , Python int.
, . , Java , , Python (?).
, PRNG , . . C libs , .
SO, - java.util.Random Python. , JavaScript, , .