27 |
|
* |
28 |
|
* <p>This class also provides additional commonly used bounded random |
29 |
|
* generation methods. |
30 |
+ |
* |
31 |
+ |
* @since 1.7 |
32 |
+ |
* @author Doug Lea |
33 |
|
*/ |
34 |
|
public class ThreadLocalRandom extends Random { |
35 |
|
// same constants as Random, but must be redeclared because private |
38 |
|
private final static long mask = (1L << 48) - 1; |
39 |
|
|
40 |
|
/** |
41 |
< |
* The random seed. We can't use super.seed |
41 |
> |
* The random seed. We can't use super.seed. |
42 |
|
*/ |
43 |
|
private long rnd; |
44 |
|
|
77 |
|
|
78 |
|
/** |
79 |
|
* Returns the current Thread's ThreadLocalRandom. |
80 |
+ |
* |
81 |
|
* @return the current Thread's ThreadLocalRandom |
82 |
|
*/ |
83 |
|
public static ThreadLocalRandom current() { |
87 |
|
/** |
88 |
|
* Throws UnsupportedOperationException. Setting seeds in this |
89 |
|
* generator is unsupported. |
90 |
+ |
* |
91 |
|
* @throws UnsupportedOperationException always |
92 |
|
*/ |
93 |
|
public void setSeed(long seed) { |
98 |
|
} |
99 |
|
|
100 |
|
protected int next(int bits) { |
101 |
< |
return (int)((rnd = (rnd * multiplier + addend) & mask) >>> (48-bits)); |
101 |
> |
rnd = (rnd * multiplier + addend) & mask; |
102 |
> |
return (int) (rnd >>> (48-bits)); |
103 |
|
} |
104 |
|
|
105 |
|
/** |
106 |
|
* Returns a pseudorandom, uniformly distributed value between the |
107 |
|
* given least value (inclusive) and bound (exclusive). |
108 |
+ |
* |
109 |
|
* @param least the least value returned |
110 |
|
* @param bound the upper bound (exclusive) |
111 |
|
* @throws IllegalArgumentException if least greater than or equal |
120 |
|
|
121 |
|
/** |
122 |
|
* Returns a pseudorandom, uniformly distributed value |
123 |
< |
* between 0 (inclusive) and the specified value (exclusive) |
123 |
> |
* between 0 (inclusive) and the specified value (exclusive). |
124 |
> |
* |
125 |
|
* @param n the bound on the random number to be returned. Must be |
126 |
|
* positive. |
127 |
|
* @return the next value |
139 |
|
while (n >= Integer.MAX_VALUE) { |
140 |
|
int bits = next(2); |
141 |
|
long half = n >>> 1; |
142 |
< |
long nextn = ((bits & 2) == 0)? half : n - half; |
142 |
> |
long nextn = ((bits & 2) == 0) ? half : n - half; |
143 |
|
if ((bits & 1) == 0) |
144 |
|
offset += n - nextn; |
145 |
|
n = nextn; |
146 |
|
} |
147 |
< |
return offset + nextInt((int)n); |
147 |
> |
return offset + nextInt((int) n); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
|
* Returns a pseudorandom, uniformly distributed value between the |
152 |
|
* given least value (inclusive) and bound (exclusive). |
153 |
+ |
* |
154 |
|
* @param least the least value returned |
155 |
|
* @param bound the upper bound (exclusive) |
156 |
|
* @return the next value |
165 |
|
|
166 |
|
/** |
167 |
|
* Returns a pseudorandom, uniformly distributed {@code double} value |
168 |
< |
* between 0 (inclusive) and the specified value (exclusive) |
168 |
> |
* between 0 (inclusive) and the specified value (exclusive). |
169 |
> |
* |
170 |
|
* @param n the bound on the random number to be returned. Must be |
171 |
|
* positive. |
172 |
|
* @return the next value |
181 |
|
/** |
182 |
|
* Returns a pseudorandom, uniformly distributed value between the |
183 |
|
* given least value (inclusive) and bound (exclusive). |
184 |
+ |
* |
185 |
|
* @param least the least value returned |
186 |
|
* @param bound the upper bound (exclusive) |
187 |
|
* @return the next value |
194 |
|
return nextDouble() * (bound - least) + least; |
195 |
|
} |
196 |
|
|
197 |
+ |
private static final long serialVersionUID = -5851777807851030925L; |
198 |
|
} |