-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXORShift.java
28 lines (22 loc) · 847 Bytes
/
XORShift.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package algorithms.numbers.bits;
import static algorithms.Assert.assertEquals;
/**
* Generate pseudo-random numbers using XOR.
* https://www.javamex.com/tutorials/random_numbers/xorshift.shtml
* https://en.wikipedia.org/wiki/Xorshift
* https://habr.com/post/183462
*/
public class XORShift {
public static long getRandom(long seed) {
seed = seed ^ seed << 21;
seed = seed ^ seed >>> 35;
return seed ^ seed << 4;
}
public static void main(String[] args) {
assertEquals(getRandom(5L), 178258005L);
assertEquals(getRandom(178258005L), 5651489766934405L);
assertEquals(getRandom(5651489766934405L), -9127299601691290113L);
assertEquals(getRandom(-9127299601691290113L),146455018630021125L);
assertEquals(getRandom(146455018630021125L), 2104002940825447L);
}
}