74 |
|
/** |
75 |
|
* Marsaglia xorshift (1, 3, 10) |
76 |
|
*/ |
77 |
< |
public static int compute6(int y) { |
78 |
< |
y ^= y << 1; |
79 |
< |
y ^= y >>> 3; |
80 |
< |
y ^= (y << 10); |
81 |
< |
return y; |
77 |
> |
public static int compute6(int seed) { |
78 |
> |
seed ^= seed << 1; |
79 |
> |
seed ^= seed >>> 3; |
80 |
> |
seed ^= (seed << 10); |
81 |
> |
return seed; |
82 |
|
} |
83 |
|
|
84 |
|
/** |
91 |
|
return y; |
92 |
|
} |
93 |
|
|
94 |
– |
public static final long compute8InitialValue = 88172645463325252L; |
94 |
|
|
95 |
|
/** |
96 |
|
* Marsaglia xorshift for longs |
102 |
|
return x; |
103 |
|
} |
104 |
|
|
105 |
+ |
public static final class XorShift32Random { |
106 |
+ |
static final AtomicInteger seq = new AtomicInteger(8862213); |
107 |
+ |
int x = -1831433054; |
108 |
+ |
public XorShift32Random(int seed) { x = seed; } |
109 |
+ |
public XorShift32Random() { |
110 |
+ |
this((int)System.nanoTime() + seq.getAndAdd(129)); |
111 |
+ |
} |
112 |
+ |
public int next() { |
113 |
+ |
x ^= x << 6; |
114 |
+ |
x ^= x >>> 21; |
115 |
+ |
x ^= (x << 7); |
116 |
+ |
return x; |
117 |
+ |
} |
118 |
+ |
} |
119 |
+ |
|
120 |
|
|
121 |
|
/** Multiplication-free RNG from Marsaglia "Xorshift RNGs" paper */ |
122 |
< |
public static class MarsagliaRandom { |
122 |
> |
public static final class MarsagliaRandom { |
123 |
> |
static final AtomicInteger seq = new AtomicInteger(3122688); |
124 |
|
int x; |
125 |
|
int y = 842502087; |
126 |
< |
int z = (int)(3579807591L & 0xffff); |
126 |
> |
int z = -715159705; |
127 |
|
int w = 273326509; |
128 |
< |
public MarsagliaRandom(int seed) { x = seed; } |
129 |
< |
public MarsagliaRandom() { this((int)System.nanoTime()); } |
128 |
> |
public MarsagliaRandom(int seed) { x = seed; } |
129 |
> |
public MarsagliaRandom() { |
130 |
> |
this((int)System.nanoTime() + seq.getAndAdd(129)); |
131 |
> |
} |
132 |
|
public int next() { |
133 |
|
int t = x ^ (x << 11); |
134 |
|
x = y; |
135 |
|
y = z; |
136 |
|
z = w; |
137 |
< |
return w = (w ^ (w >>> 19)) ^ (t ^ (t >>> 8)); |
137 |
> |
return w = (w ^ (w >>> 19) ^ (t ^ (t >>> 8))); |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* An actually useful random number generator, but unsynchronized. |
126 |
< |
* Basically same as java.util.Random. |
142 |
> |
* Unsynchronized version of java.util.Random algorithm. |
143 |
|
*/ |
144 |
< |
public static class SimpleRandom { |
144 |
> |
public static final class SimpleRandom { |
145 |
|
private final static long multiplier = 0x5DEECE66DL; |
146 |
|
private final static long addend = 0xBL; |
147 |
|
private final static long mask = (1L << 48) - 1; |
148 |
< |
static final AtomicLong seq = new AtomicLong(1); |
149 |
< |
private long seed = System.nanoTime() + seq.getAndIncrement(); |
148 |
> |
static final AtomicLong seq = new AtomicLong( -715159705); |
149 |
> |
private long seed; |
150 |
> |
|
151 |
> |
SimpleRandom(long s) { |
152 |
> |
seed = s; |
153 |
> |
} |
154 |
> |
|
155 |
> |
SimpleRandom() { |
156 |
> |
seed = System.nanoTime() + seq.getAndAdd(129); |
157 |
> |
} |
158 |
|
|
159 |
|
public void setSeed(long s) { |
160 |
|
seed = s; |