jaroslav@1890
|
1 |
/*
|
jaroslav@1890
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@1890
|
3 |
*
|
jaroslav@1890
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@1890
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@1890
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@1890
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@1890
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@1890
|
9 |
*
|
jaroslav@1890
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@1890
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@1890
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@1890
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@1890
|
14 |
* accompanied this code).
|
jaroslav@1890
|
15 |
*
|
jaroslav@1890
|
16 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@1890
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@1890
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@1890
|
19 |
*
|
jaroslav@1890
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@1890
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@1890
|
22 |
* questions.
|
jaroslav@1890
|
23 |
*/
|
jaroslav@1890
|
24 |
|
jaroslav@1890
|
25 |
/*
|
jaroslav@1890
|
26 |
* This file is available under and governed by the GNU General Public
|
jaroslav@1890
|
27 |
* License version 2 only, as published by the Free Software Foundation.
|
jaroslav@1890
|
28 |
* However, the following notice accompanied the original version of this
|
jaroslav@1890
|
29 |
* file:
|
jaroslav@1890
|
30 |
*
|
jaroslav@1890
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
jaroslav@1890
|
32 |
* Expert Group and released to the public domain, as explained at
|
jaroslav@1890
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
jaroslav@1890
|
34 |
*/
|
jaroslav@1890
|
35 |
|
jaroslav@1890
|
36 |
package java.util.concurrent;
|
jaroslav@1890
|
37 |
|
jaroslav@1890
|
38 |
import java.util.Random;
|
jaroslav@1890
|
39 |
|
jaroslav@1890
|
40 |
/**
|
jaroslav@1890
|
41 |
* A random number generator isolated to the current thread. Like the
|
jaroslav@1890
|
42 |
* global {@link java.util.Random} generator used by the {@link
|
jaroslav@1890
|
43 |
* java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
|
jaroslav@1890
|
44 |
* with an internally generated seed that may not otherwise be
|
jaroslav@1890
|
45 |
* modified. When applicable, use of {@code ThreadLocalRandom} rather
|
jaroslav@1890
|
46 |
* than shared {@code Random} objects in concurrent programs will
|
jaroslav@1890
|
47 |
* typically encounter much less overhead and contention. Use of
|
jaroslav@1890
|
48 |
* {@code ThreadLocalRandom} is particularly appropriate when multiple
|
jaroslav@1890
|
49 |
* tasks (for example, each a {@link ForkJoinTask}) use random numbers
|
jaroslav@1890
|
50 |
* in parallel in thread pools.
|
jaroslav@1890
|
51 |
*
|
jaroslav@1890
|
52 |
* <p>Usages of this class should typically be of the form:
|
jaroslav@1890
|
53 |
* {@code ThreadLocalRandom.current().nextX(...)} (where
|
jaroslav@1890
|
54 |
* {@code X} is {@code Int}, {@code Long}, etc).
|
jaroslav@1890
|
55 |
* When all usages are of this form, it is never possible to
|
jaroslav@1890
|
56 |
* accidently share a {@code ThreadLocalRandom} across multiple threads.
|
jaroslav@1890
|
57 |
*
|
jaroslav@1890
|
58 |
* <p>This class also provides additional commonly used bounded random
|
jaroslav@1890
|
59 |
* generation methods.
|
jaroslav@1890
|
60 |
*
|
jaroslav@1890
|
61 |
* @since 1.7
|
jaroslav@1890
|
62 |
* @author Doug Lea
|
jaroslav@1890
|
63 |
*/
|
jaroslav@1890
|
64 |
public class ThreadLocalRandom extends Random {
|
jaroslav@1890
|
65 |
// same constants as Random, but must be redeclared because private
|
jaroslav@1890
|
66 |
private static final long multiplier = 0x5DEECE66DL;
|
jaroslav@1890
|
67 |
private static final long addend = 0xBL;
|
jaroslav@1890
|
68 |
private static final long mask = (1L << 48) - 1;
|
jaroslav@1890
|
69 |
|
jaroslav@1890
|
70 |
/**
|
jaroslav@1890
|
71 |
* The random seed. We can't use super.seed.
|
jaroslav@1890
|
72 |
*/
|
jaroslav@1890
|
73 |
private long rnd;
|
jaroslav@1890
|
74 |
|
jaroslav@1890
|
75 |
/**
|
jaroslav@1890
|
76 |
* Initialization flag to permit calls to setSeed to succeed only
|
jaroslav@1890
|
77 |
* while executing the Random constructor. We can't allow others
|
jaroslav@1890
|
78 |
* since it would cause setting seed in one part of a program to
|
jaroslav@1890
|
79 |
* unintentionally impact other usages by the thread.
|
jaroslav@1890
|
80 |
*/
|
jaroslav@1890
|
81 |
boolean initialized;
|
jaroslav@1890
|
82 |
|
jaroslav@1890
|
83 |
// Padding to help avoid memory contention among seed updates in
|
jaroslav@1890
|
84 |
// different TLRs in the common case that they are located near
|
jaroslav@1890
|
85 |
// each other.
|
jaroslav@1890
|
86 |
private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
|
jaroslav@1890
|
87 |
|
jaroslav@1890
|
88 |
/**
|
jaroslav@1890
|
89 |
* The actual ThreadLocal
|
jaroslav@1890
|
90 |
*/
|
jaroslav@1890
|
91 |
private static final ThreadLocal<ThreadLocalRandom> localRandom =
|
jaroslav@1890
|
92 |
new ThreadLocal<ThreadLocalRandom>() {
|
jaroslav@1890
|
93 |
protected ThreadLocalRandom initialValue() {
|
jaroslav@1890
|
94 |
return new ThreadLocalRandom();
|
jaroslav@1890
|
95 |
}
|
jaroslav@1890
|
96 |
};
|
jaroslav@1890
|
97 |
|
jaroslav@1890
|
98 |
|
jaroslav@1890
|
99 |
/**
|
jaroslav@1890
|
100 |
* Constructor called only by localRandom.initialValue.
|
jaroslav@1890
|
101 |
*/
|
jaroslav@1890
|
102 |
ThreadLocalRandom() {
|
jaroslav@1890
|
103 |
super();
|
jaroslav@1890
|
104 |
initialized = true;
|
jaroslav@1890
|
105 |
}
|
jaroslav@1890
|
106 |
|
jaroslav@1890
|
107 |
/**
|
jaroslav@1890
|
108 |
* Returns the current thread's {@code ThreadLocalRandom}.
|
jaroslav@1890
|
109 |
*
|
jaroslav@1890
|
110 |
* @return the current thread's {@code ThreadLocalRandom}
|
jaroslav@1890
|
111 |
*/
|
jaroslav@1890
|
112 |
public static ThreadLocalRandom current() {
|
jaroslav@1890
|
113 |
return localRandom.get();
|
jaroslav@1890
|
114 |
}
|
jaroslav@1890
|
115 |
|
jaroslav@1890
|
116 |
/**
|
jaroslav@1890
|
117 |
* Throws {@code UnsupportedOperationException}. Setting seeds in
|
jaroslav@1890
|
118 |
* this generator is not supported.
|
jaroslav@1890
|
119 |
*
|
jaroslav@1890
|
120 |
* @throws UnsupportedOperationException always
|
jaroslav@1890
|
121 |
*/
|
jaroslav@1890
|
122 |
public void setSeed(long seed) {
|
jaroslav@1890
|
123 |
if (initialized)
|
jaroslav@1890
|
124 |
throw new UnsupportedOperationException();
|
jaroslav@1890
|
125 |
rnd = (seed ^ multiplier) & mask;
|
jaroslav@1890
|
126 |
}
|
jaroslav@1890
|
127 |
|
jaroslav@1890
|
128 |
protected int next(int bits) {
|
jaroslav@1890
|
129 |
rnd = (rnd * multiplier + addend) & mask;
|
jaroslav@1890
|
130 |
return (int) (rnd >>> (48-bits));
|
jaroslav@1890
|
131 |
}
|
jaroslav@1890
|
132 |
|
jaroslav@1890
|
133 |
/**
|
jaroslav@1890
|
134 |
* Returns a pseudorandom, uniformly distributed value between the
|
jaroslav@1890
|
135 |
* given least value (inclusive) and bound (exclusive).
|
jaroslav@1890
|
136 |
*
|
jaroslav@1890
|
137 |
* @param least the least value returned
|
jaroslav@1890
|
138 |
* @param bound the upper bound (exclusive)
|
jaroslav@1890
|
139 |
* @throws IllegalArgumentException if least greater than or equal
|
jaroslav@1890
|
140 |
* to bound
|
jaroslav@1890
|
141 |
* @return the next value
|
jaroslav@1890
|
142 |
*/
|
jaroslav@1890
|
143 |
public int nextInt(int least, int bound) {
|
jaroslav@1890
|
144 |
if (least >= bound)
|
jaroslav@1890
|
145 |
throw new IllegalArgumentException();
|
jaroslav@1890
|
146 |
return nextInt(bound - least) + least;
|
jaroslav@1890
|
147 |
}
|
jaroslav@1890
|
148 |
|
jaroslav@1890
|
149 |
/**
|
jaroslav@1890
|
150 |
* Returns a pseudorandom, uniformly distributed value
|
jaroslav@1890
|
151 |
* between 0 (inclusive) and the specified value (exclusive).
|
jaroslav@1890
|
152 |
*
|
jaroslav@1890
|
153 |
* @param n the bound on the random number to be returned. Must be
|
jaroslav@1890
|
154 |
* positive.
|
jaroslav@1890
|
155 |
* @return the next value
|
jaroslav@1890
|
156 |
* @throws IllegalArgumentException if n is not positive
|
jaroslav@1890
|
157 |
*/
|
jaroslav@1890
|
158 |
public long nextLong(long n) {
|
jaroslav@1890
|
159 |
if (n <= 0)
|
jaroslav@1890
|
160 |
throw new IllegalArgumentException("n must be positive");
|
jaroslav@1890
|
161 |
// Divide n by two until small enough for nextInt. On each
|
jaroslav@1890
|
162 |
// iteration (at most 31 of them but usually much less),
|
jaroslav@1890
|
163 |
// randomly choose both whether to include high bit in result
|
jaroslav@1890
|
164 |
// (offset) and whether to continue with the lower vs upper
|
jaroslav@1890
|
165 |
// half (which makes a difference only if odd).
|
jaroslav@1890
|
166 |
long offset = 0;
|
jaroslav@1890
|
167 |
while (n >= Integer.MAX_VALUE) {
|
jaroslav@1890
|
168 |
int bits = next(2);
|
jaroslav@1890
|
169 |
long half = n >>> 1;
|
jaroslav@1890
|
170 |
long nextn = ((bits & 2) == 0) ? half : n - half;
|
jaroslav@1890
|
171 |
if ((bits & 1) == 0)
|
jaroslav@1890
|
172 |
offset += n - nextn;
|
jaroslav@1890
|
173 |
n = nextn;
|
jaroslav@1890
|
174 |
}
|
jaroslav@1890
|
175 |
return offset + nextInt((int) n);
|
jaroslav@1890
|
176 |
}
|
jaroslav@1890
|
177 |
|
jaroslav@1890
|
178 |
/**
|
jaroslav@1890
|
179 |
* Returns a pseudorandom, uniformly distributed value between the
|
jaroslav@1890
|
180 |
* given least value (inclusive) and bound (exclusive).
|
jaroslav@1890
|
181 |
*
|
jaroslav@1890
|
182 |
* @param least the least value returned
|
jaroslav@1890
|
183 |
* @param bound the upper bound (exclusive)
|
jaroslav@1890
|
184 |
* @return the next value
|
jaroslav@1890
|
185 |
* @throws IllegalArgumentException if least greater than or equal
|
jaroslav@1890
|
186 |
* to bound
|
jaroslav@1890
|
187 |
*/
|
jaroslav@1890
|
188 |
public long nextLong(long least, long bound) {
|
jaroslav@1890
|
189 |
if (least >= bound)
|
jaroslav@1890
|
190 |
throw new IllegalArgumentException();
|
jaroslav@1890
|
191 |
return nextLong(bound - least) + least;
|
jaroslav@1890
|
192 |
}
|
jaroslav@1890
|
193 |
|
jaroslav@1890
|
194 |
/**
|
jaroslav@1890
|
195 |
* Returns a pseudorandom, uniformly distributed {@code double} value
|
jaroslav@1890
|
196 |
* between 0 (inclusive) and the specified value (exclusive).
|
jaroslav@1890
|
197 |
*
|
jaroslav@1890
|
198 |
* @param n the bound on the random number to be returned. Must be
|
jaroslav@1890
|
199 |
* positive.
|
jaroslav@1890
|
200 |
* @return the next value
|
jaroslav@1890
|
201 |
* @throws IllegalArgumentException if n is not positive
|
jaroslav@1890
|
202 |
*/
|
jaroslav@1890
|
203 |
public double nextDouble(double n) {
|
jaroslav@1890
|
204 |
if (n <= 0)
|
jaroslav@1890
|
205 |
throw new IllegalArgumentException("n must be positive");
|
jaroslav@1890
|
206 |
return nextDouble() * n;
|
jaroslav@1890
|
207 |
}
|
jaroslav@1890
|
208 |
|
jaroslav@1890
|
209 |
/**
|
jaroslav@1890
|
210 |
* Returns a pseudorandom, uniformly distributed value between the
|
jaroslav@1890
|
211 |
* given least value (inclusive) and bound (exclusive).
|
jaroslav@1890
|
212 |
*
|
jaroslav@1890
|
213 |
* @param least the least value returned
|
jaroslav@1890
|
214 |
* @param bound the upper bound (exclusive)
|
jaroslav@1890
|
215 |
* @return the next value
|
jaroslav@1890
|
216 |
* @throws IllegalArgumentException if least greater than or equal
|
jaroslav@1890
|
217 |
* to bound
|
jaroslav@1890
|
218 |
*/
|
jaroslav@1890
|
219 |
public double nextDouble(double least, double bound) {
|
jaroslav@1890
|
220 |
if (least >= bound)
|
jaroslav@1890
|
221 |
throw new IllegalArgumentException();
|
jaroslav@1890
|
222 |
return nextDouble() * (bound - least) + least;
|
jaroslav@1890
|
223 |
}
|
jaroslav@1890
|
224 |
|
jaroslav@1890
|
225 |
private static final long serialVersionUID = -5851777807851030925L;
|
jaroslav@1890
|
226 |
}
|