1 |
+ |
/* |
2 |
+ |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
+ |
* Expert Group and released to the public domain, as explained at |
4 |
+ |
* http://creativecommons.org/licenses/publicdomain |
5 |
+ |
*/ |
6 |
|
/** |
7 |
|
* Misc utilities in JSR166 performance tests |
8 |
|
*/ |
71 |
|
return 36969 * (x & 65535) + (x >> 16); |
72 |
|
} |
73 |
|
|
74 |
+ |
/** |
75 |
+ |
* Marsaglia xorshift (1, 3, 10) |
76 |
+ |
*/ |
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 |
+ |
/** |
85 |
+ |
* Marsaglia xorshift (6, 21, 7) |
86 |
+ |
*/ |
87 |
+ |
public static int compute7(int y) { |
88 |
+ |
y ^= y << 6; |
89 |
+ |
y ^= y >>> 21; |
90 |
+ |
y ^= (y << 7); |
91 |
+ |
return y; |
92 |
+ |
} |
93 |
+ |
|
94 |
+ |
|
95 |
+ |
/** |
96 |
+ |
* Marsaglia xorshift for longs |
97 |
+ |
*/ |
98 |
+ |
public static long compute8(long x) { |
99 |
+ |
x ^= x << 13; |
100 |
+ |
x ^= x >>> 7; |
101 |
+ |
x ^= (x << 17); |
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 final class MarsagliaRandom { |
123 |
+ |
static final AtomicInteger seq = new AtomicInteger(3122688); |
124 |
+ |
int x; |
125 |
+ |
int y = 842502087; |
126 |
+ |
int z = -715159705; |
127 |
+ |
int w = 273326509; |
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))); |
138 |
+ |
} |
139 |
+ |
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* An actually useful random number generator, but unsynchronized. |
72 |
< |
* 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; |