18 |
|
* contention. ThreadLocalRandoms are particularly appropriate when |
19 |
|
* multiple tasks (for example, each a {@link ForkJoinTask}), use |
20 |
|
* random numbers in parallel in thread pools. |
21 |
< |
* |
21 |
> |
* |
22 |
|
* <p>Usages of this class should typically be of the form: |
23 |
< |
* <code>ThreadLocalRandom.current().nextX(...)</code> (where |
24 |
< |
* <code>X</code> is <code>Int</code>, <code>Long</code>, etc). |
23 |
> |
* {@code ThreadLocalRandom.current().nextX(...)} (where |
24 |
> |
* {@code X} is {@code Int}, {@code Long}, etc). |
25 |
|
* When all usages are of this form, it is never possible to |
26 |
|
* accidently share ThreadLocalRandoms across multiple threads. |
27 |
|
* |
37 |
|
/** |
38 |
|
* The random seed. We can't use super.seed |
39 |
|
*/ |
40 |
< |
private long rnd; |
40 |
> |
private long rnd; |
41 |
|
|
42 |
|
/** |
43 |
|
* Initialization flag to permit the first and only allowed call |
65 |
|
|
66 |
|
/** |
67 |
|
* Constructor called only by localRandom.initialValue. |
68 |
< |
* We rely on the fact that the superclass no-arg constructor |
68 |
> |
* We rely on the fact that the superclass no-arg constructor |
69 |
|
* invokes setSeed exactly once to initialize. |
70 |
|
*/ |
71 |
|
ThreadLocalRandom() { |
83 |
|
/** |
84 |
|
* Throws UnsupportedOperationException. Setting seeds in this |
85 |
|
* generator is unsupported. |
86 |
< |
* @throw UnsupportedOperationException always |
86 |
> |
* @throws UnsupportedOperationException always |
87 |
|
*/ |
88 |
< |
public void setSeed(long seed) { |
88 |
> |
public void setSeed(long seed) { |
89 |
|
if (initialized) |
90 |
|
throw new UnsupportedOperationException(); |
91 |
|
initialized = true; |
126 |
|
// iteration (at most 31 of them but usually much less), |
127 |
|
// randomly choose both whether to include high bit in result |
128 |
|
// (offset) and whether to continue with the lower vs upper |
129 |
< |
// half (which makes a difference only if odd). |
129 |
> |
// half (which makes a difference only if odd). |
130 |
|
long offset = 0; |
131 |
|
while (n >= Integer.MAX_VALUE) { |
132 |
< |
int bits = next(2); |
132 |
> |
int bits = next(2); |
133 |
|
long half = n >>> 1; |
134 |
|
long nextn = ((bits & 2) == 0)? half : n - half; |
135 |
|
if ((bits & 1) == 0) |
183 |
|
return nextDouble() * (bound - least) + least; |
184 |
|
} |
185 |
|
|
186 |
< |
} |
186 |
> |
} |