17 |
|
// Some mindless computation to do between synchronizations... |
18 |
|
|
19 |
|
/** |
20 |
< |
* generates 32 bit pseudo-random numbers. |
20 |
> |
* generates 32 bit pseudo-random numbers. |
21 |
|
* Adapted from http://www.snippets.org |
22 |
|
*/ |
23 |
< |
public static int compute1(int x) { |
23 |
> |
public static int compute1(int x) { |
24 |
|
int lo = 16807 * (x & 0xFFFF); |
25 |
|
int hi = 16807 * (x >>> 16); |
26 |
|
lo += (hi & 0x7FFF) << 16; |
40 |
|
* Computes a linear congruential random number a random number |
41 |
|
* of times. |
42 |
|
*/ |
43 |
< |
public static int compute2(int x) { |
43 |
> |
public static int compute2(int x) { |
44 |
|
int loops = (x >>> 4) & 7; |
45 |
|
while (loops-- > 0) { |
46 |
|
x = (x * 2147483647) % 16807; |
51 |
|
/** |
52 |
|
* Yet another random number generator |
53 |
|
*/ |
54 |
< |
public static int compute3(int x) { |
54 |
> |
public static int compute3(int x) { |
55 |
|
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
56 |
< |
return (t > 0)? t : t + 0x7fffffff; |
56 |
> |
return (t > 0) ? t : t + 0x7fffffff; |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
|
* Yet another random number generator |
61 |
|
*/ |
62 |
< |
public static int compute4(int x) { |
62 |
> |
public static int compute4(int x) { |
63 |
|
return x * 134775813 + 1; |
64 |
|
} |
65 |
|
|
67 |
|
/** |
68 |
|
* Yet another random number generator |
69 |
|
*/ |
70 |
< |
public static int compute5(int x) { |
70 |
> |
public static int compute5(int x) { |
71 |
|
return 36969 * (x & 65535) + (x >> 16); |
72 |
|
} |
73 |
|
|
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 |
|
/** |
85 |
|
* Marsaglia xorshift (6, 21, 7) |
86 |
|
*/ |
87 |
|
public static int compute7(int y) { |
88 |
< |
y ^= y << 6; |
89 |
< |
y ^= y >>> 21; |
88 |
> |
y ^= y << 6; |
89 |
> |
y ^= y >>> 21; |
90 |
|
y ^= (y << 7); |
91 |
|
return y; |
92 |
|
} |
93 |
|
|
94 |
< |
public static final long compute8InitialValue = 88172645463325252L; |
94 |
> |
// FNV: (x ^ 0x811c9dc5) * 0x01000193; 15485863; |
95 |
|
|
96 |
|
/** |
97 |
|
* Marsaglia xorshift for longs |
98 |
|
*/ |
99 |
< |
public static long compute8(long x) { |
100 |
< |
x ^= x << 13; |
101 |
< |
x ^= x >>> 7; |
99 |
> |
public static long compute8(long x) { |
100 |
> |
x ^= x << 13; |
101 |
> |
x ^= x >>> 7; |
102 |
|
x ^= (x << 17); |
103 |
|
return x; |
104 |
|
} |
105 |
|
|
106 |
+ |
public static final class XorShift32Random { |
107 |
+ |
static final AtomicInteger seq = new AtomicInteger(8862213); |
108 |
+ |
int x = -1831433054; |
109 |
+ |
public XorShift32Random(int seed) { x = seed; } |
110 |
+ |
public XorShift32Random() { |
111 |
+ |
this((int) System.nanoTime() + seq.getAndAdd(129)); |
112 |
+ |
} |
113 |
+ |
public int next() { |
114 |
+ |
x ^= x << 6; |
115 |
+ |
x ^= x >>> 21; |
116 |
+ |
x ^= (x << 7); |
117 |
+ |
return x; |
118 |
+ |
} |
119 |
+ |
} |
120 |
+ |
|
121 |
|
|
122 |
|
/** Multiplication-free RNG from Marsaglia "Xorshift RNGs" paper */ |
123 |
< |
public static class MarsagliaRandom { |
123 |
> |
public static final class MarsagliaRandom { |
124 |
> |
static final AtomicInteger seq = new AtomicInteger(3122688); |
125 |
|
int x; |
126 |
|
int y = 842502087; |
127 |
< |
int z = (int)(3579807591L & 0xffff); |
127 |
> |
int z = -715159705; |
128 |
|
int w = 273326509; |
129 |
< |
public MarsagliaRandom(int seed) { x = seed; } |
130 |
< |
public MarsagliaRandom() { this((int)System.nanoTime()); } |
129 |
> |
public MarsagliaRandom(int seed) { x = seed; } |
130 |
> |
public MarsagliaRandom() { |
131 |
> |
this((int) System.nanoTime() + seq.getAndAdd(129)); |
132 |
> |
} |
133 |
|
public int next() { |
134 |
|
int t = x ^ (x << 11); |
135 |
< |
x = y; |
136 |
< |
y = z; |
135 |
> |
x = y; |
136 |
> |
y = z; |
137 |
|
z = w; |
138 |
< |
return w = (w ^ (w >>> 19)) ^ (t ^ (t >>> 8)); |
138 |
> |
return w = (w ^ (w >>> 19) ^ (t ^ (t >>> 8))); |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
< |
* An actually useful random number generator, but unsynchronized. |
126 |
< |
* Basically same as java.util.Random. |
143 |
> |
* Unsynchronized version of java.util.Random algorithm. |
144 |
|
*/ |
145 |
< |
public static class SimpleRandom { |
146 |
< |
private final static long multiplier = 0x5DEECE66DL; |
147 |
< |
private final static long addend = 0xBL; |
148 |
< |
private final static long mask = (1L << 48) - 1; |
149 |
< |
static final AtomicLong seq = new AtomicLong(1); |
150 |
< |
private long seed = System.nanoTime() + seq.getAndIncrement(); |
145 |
> |
public static final class SimpleRandom { |
146 |
> |
private static final long multiplier = 0x5DEECE66DL; |
147 |
> |
private static final long addend = 0xBL; |
148 |
> |
private static final long mask = (1L << 48) - 1; |
149 |
> |
static final AtomicLong seq = new AtomicLong( -715159705); |
150 |
> |
private long seed; |
151 |
> |
|
152 |
> |
SimpleRandom(long s) { |
153 |
> |
seed = s; |
154 |
> |
} |
155 |
> |
|
156 |
> |
SimpleRandom() { |
157 |
> |
seed = System.nanoTime() + seq.getAndAdd(129); |
158 |
> |
} |
159 |
|
|
160 |
|
public void setSeed(long s) { |
161 |
|
seed = s; |
164 |
|
public int next() { |
165 |
|
long nextseed = (seed * multiplier + addend) & mask; |
166 |
|
seed = nextseed; |
167 |
< |
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF; |
167 |
> |
return ((int) (nextseed >>> 17)) & 0x7FFFFFFF; |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
|
public static class BarrierTimer implements Runnable { |
172 |
< |
public volatile long startTime; |
173 |
< |
public volatile long endTime; |
172 |
> |
volatile boolean started; |
173 |
> |
volatile long startTime; |
174 |
> |
volatile long endTime; |
175 |
|
public void run() { |
176 |
|
long t = System.nanoTime(); |
177 |
< |
if (startTime == 0) |
177 |
> |
if (!started) { |
178 |
> |
started = true; |
179 |
|
startTime = t; |
180 |
< |
else |
180 |
> |
} else |
181 |
|
endTime = t; |
182 |
|
} |
183 |
|
public void clear() { |
184 |
< |
startTime = 0; |
158 |
< |
endTime = 0; |
184 |
> |
started = false; |
185 |
|
} |
186 |
|
public long getTime() { |
187 |
|
return endTime - startTime; |
198 |
|
b.replace(b.length()-num.length(), b.length(), num); |
199 |
|
return b.toString(); |
200 |
|
} |
201 |
< |
|
201 |
> |
|
202 |
|
} |