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