If you think the Android project Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2014./*fromwww.java2s.com*/
*
* BaasBox - info@baasbox.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/package com.baasbox.android.samples.phrasebook.utils;
import java.util.Random;
/**
* Created by Andrea Tortorella on 09/09/14.
*/publicclass ThreadLocalRandom extends Random {
privatestaticfinallong MULTIPLIER = 0x5DEECE66DL;
privatestaticfinallong ADDEND = 0xBL;
privatestaticfinallong MASK = (1L << 48) -1;
privatelong rnd;
boolean initialized;
privatelong pad0,pad1,pad2,pad3,pad4,pad5,pad6,pad7;
privatestaticfinal ThreadLocal<ThreadLocalRandom> LOCAL_RANDOM =
new ThreadLocal<ThreadLocalRandom>() {
@Override
protected ThreadLocalRandom initialValue() {
returnnew ThreadLocalRandom();
}
};
ThreadLocalRandom(){
super();
initialized = true;
}
publicstatic ThreadLocalRandom current(){
return LOCAL_RANDOM.get();
}
publicvoid setSeed(long seed) {
if (initialized){
thrownew UnsupportedOperationException();
}
rnd = (seed ^ MULTIPLIER) & MASK;
}
protectedint next(int bits) {
rnd = (rnd * MULTIPLIER + ADDEND)& MASK;
return (int) (rnd >>> (48-bits));
}
publicint nextInt(int least,int bound) {
if (least >= bound){
thrownew IllegalArgumentException();
}
return nextInt(bound-least)+least;
}
publiclong nextLong(long n) {
if (n <= 0) {
thrownew IllegalArgumentException("n must be positive");
}
long offset = 0;
while (n >= Integer.MAX_VALUE) {
int bits = next(2);
long half = n >>> 1;
long nextn = ((bits & 2) == 0) ? half : n-half;
if ((bits & 1) == 0) {
offset += n -nextn;
}
n = nextn;
}
return offset + nextInt((int)n);
}
publiclong nextLong(long least,long bound){
if (least >= bound){
thrownew IllegalArgumentException();
}
return nextLong((bound-least)) + least;
}
publicdouble nextDouble(double n) {
if (n <= 0) {
thrownew IllegalArgumentException("n must be positive");
}
return nextDouble()*n;
}
publicdouble nextDouble(double least,double bound) {
if (least>=bound){
thrownew IllegalArgumentException();
}
return nextDouble()*(bound-least)+least;
}
privatestaticfinallong serialVersionUID = -5851777807851030925L;
}