1771N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1771N/A * This code is free software; you can redistribute it and/or modify it 1771N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 1771N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 1771N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1771N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1771N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1771N/A * version 2 for more details (a copy is included in the LICENSE file that 1771N/A * You should have received a copy of the GNU General Public License version 1771N/A * 2 along with this work; if not, write to the Free Software Foundation, 1771N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 1771N/A * This file is available under and governed by the GNU General Public 1771N/A * License version 2 only, as published by the Free Software Foundation. 1771N/A * However, the following notice accompanied the original version of this 1771N/A * Written by Doug Lea with assistance from members of JCP JSR-166 1771N/A * Expert Group and released to the public domain, as explained at 1771N/A * A random number generator isolated to the current thread. Like the 1771N/A * global {@link java.util.Random} generator used by the {@link 1771N/A * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized 1771N/A * with an internally generated seed that may not otherwise be 1771N/A * modified. When applicable, use of {@code ThreadLocalRandom} rather 1771N/A * than shared {@code Random} objects in concurrent programs will 1771N/A * typically encounter much less overhead and contention. Use of 1771N/A * {@code ThreadLocalRandom} is particularly appropriate when multiple 1771N/A * tasks (for example, each a {@link ForkJoinTask}) use random numbers 1771N/A * in parallel in thread pools. 1771N/A * <p>Usages of this class should typically be of the form: 1771N/A * {@code ThreadLocalRandom.current().nextX(...)} (where 1771N/A * {@code X} is {@code Int}, {@code Long}, etc). 1771N/A * When all usages are of this form, it is never possible to 1771N/A * accidently share a {@code ThreadLocalRandom} across multiple threads. 1771N/A * <p>This class also provides additional commonly used bounded random 1771N/A // same constants as Random, but must be redeclared because private 3203N/A private static final long mask = (
1L <<
48) -
1;
1771N/A * The random seed. We can't use super.seed. 2460N/A * Initialization flag to permit calls to setSeed to succeed only 2460N/A * while executing the Random constructor. We can't allow others 2460N/A * since it would cause setting seed in one part of a program to 2460N/A * unintentionally impact other usages by the thread. 1771N/A // Padding to help avoid memory contention among seed updates in 1771N/A // different TLRs in the common case that they are located near 1771N/A * Returns the current thread's {@code ThreadLocalRandom}. 1771N/A * @return the current thread's {@code ThreadLocalRandom} 1771N/A * Throws {@code UnsupportedOperationException}. Setting seeds in 1771N/A * this generator is not supported. 1771N/A * @throws UnsupportedOperationException always 1771N/A * Returns a pseudorandom, uniformly distributed value between the 1771N/A * given least value (inclusive) and bound (exclusive). 1771N/A * @param least the least value returned 1771N/A * @param bound the upper bound (exclusive) 1771N/A * @throws IllegalArgumentException if least greater than or equal 1771N/A * Returns a pseudorandom, uniformly distributed value 1771N/A * between 0 (inclusive) and the specified value (exclusive). 1771N/A * @param n the bound on the random number to be returned. Must be 1771N/A * @throws IllegalArgumentException if n is not positive 1771N/A // Divide n by two until small enough for nextInt. On each 1771N/A // iteration (at most 31 of them but usually much less), 1771N/A // randomly choose both whether to include high bit in result 1771N/A // (offset) and whether to continue with the lower vs upper 1771N/A // half (which makes a difference only if odd). 1771N/A * Returns a pseudorandom, uniformly distributed value between the 1771N/A * given least value (inclusive) and bound (exclusive). 1771N/A * @param least the least value returned 1771N/A * @param bound the upper bound (exclusive) 1771N/A * @throws IllegalArgumentException if least greater than or equal 1771N/A * Returns a pseudorandom, uniformly distributed {@code double} value 1771N/A * between 0 (inclusive) and the specified value (exclusive). 1771N/A * @param n the bound on the random number to be returned. Must be 1771N/A * @throws IllegalArgumentException if n is not positive 1771N/A * Returns a pseudorandom, uniformly distributed value between the 1771N/A * given least value (inclusive) and bound (exclusive). 1771N/A * @param least the least value returned 1771N/A * @param bound the upper bound (exclusive) 1771N/A * @throws IllegalArgumentException if least greater than or equal