Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Written by Doug Lea with assistance from members of JCP JSR-166
   * Expert Group and released to the public domain, as explained at
   * http://creativecommons.org/publicdomain/zero/1.0/
   *
   * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java?view=markup
   */
  
  package com.yammer.metrics.stats;
 
 
 // CHECKSTYLE:OFF
Copied directly from the JSR-166 project.
 
 @SuppressWarnings("UnusedDeclaration")
 class ThreadLocalRandom extends Random {
     // same constants as Random, but must be redeclared because private
     private static final long multiplier = 0x5DEECE66DL;
     private static final long addend = 0xBL;
     private static final long mask = (1L << 48) - 1;

    
The random seed. We can't use super.seed.
 
     private long rnd;

    
Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor. We can't allow others since it would cause setting seed in one part of a program to unintentionally impact other usages by the thread.
 
     boolean initialized;
 
     // Padding to help avoid memory contention among seed updates in
     // different TLRs in the common case that they are located near
     // each other.
     private long pad0pad1pad2pad3pad4pad5pad6pad7;

    
The actual ThreadLocal
 
     private static final ThreadLocal<ThreadLocalRandomlocalRandom =
             new ThreadLocal<ThreadLocalRandom>() {
                 protected ThreadLocalRandom initialValue() {
                     return new ThreadLocalRandom();
                 }
             };


    
Constructor called only by localRandom.initialValue.
 
     ThreadLocalRandom() {
         super();
          = true;
     }

    
Returns the current thread's ThreadLocalRandom.

Returns:
the current thread's ThreadLocalRandom
 
     public static ThreadLocalRandom current() {
         return .get();
     }

    
Throws UnsupportedOperationException. Setting seeds in this generator is not supported.

 
     public void setSeed(long seed) {
         if ()
             throw new UnsupportedOperationException();
          = (seed ^ ) & ;
     }
 
     protected int next(int bits) {
          = ( *  + ) & ;
         return (int) ( >>> (48 - bits));
     }

    
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters:
least the least value returned
bound the upper bound (exclusive)
Returns:
the next value
Throws:
java.lang.IllegalArgumentException if least greater than or equal to bound
 
     public int nextInt(int leastint bound) {
         if (least >= bound)
             throw new IllegalArgumentException();
         return nextInt(bound - least) + least;
     }

    
Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).

Parameters:
n the bound on the random number to be returned. Must be positive.
Returns:
the next value
Throws:
java.lang.IllegalArgumentException if n is not positive
    public long nextLong(long n) {
        if (n <= 0)
            throw new IllegalArgumentException("n must be positive");
        // Divide n by two until small enough for nextInt. On each
        // iteration (at most 31 of them but usually much less),
        // randomly choose both whether to include high bit in result
        // (offset) and whether to continue with the lower vs upper
        // half (which makes a difference only if odd).
        long offset = 0;
        while (n >= .) {
            final int bits = next(2);
            final long half = n >>> 1;
            final long nextn = ((bits & 2) == 0) ? half : n - half;
            if ((bits & 1) == 0)
                offset += n - nextn;
            n = nextn;
        }
        return offset + nextInt((intn);
    }

    
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters:
least the least value returned
bound the upper bound (exclusive)
Returns:
the next value
Throws:
java.lang.IllegalArgumentException if least greater than or equal to bound
    public long nextLong(long leastlong bound) {
        if (least >= bound)
            throw new IllegalArgumentException();
        return nextLong(bound - least) + least;
    }

    
Returns a pseudorandom, uniformly distributed double value between 0 (inclusive) and the specified value (exclusive).

Parameters:
n the bound on the random number to be returned. Must be positive.
Returns:
the next value
Throws:
java.lang.IllegalArgumentException if n is not positive
    public double nextDouble(double n) {
        if (n <= 0)
            throw new IllegalArgumentException("n must be positive");
        return nextDouble() * n;
    }

    
Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters:
least the least value returned
bound the upper bound (exclusive)
Returns:
the next value
Throws:
java.lang.IllegalArgumentException if least greater than or equal to bound
    public double nextDouble(double leastdouble bound) {
        if (least >= bound)
            throw new IllegalArgumentException();
        return nextDouble() * (bound - least) + least;
    }
    private static final long serialVersionUID = -5851777807851030925L;
// CHECKSTYLE:ON
New to GrepCode? Check out our FAQ X