Dropped new ThreadLocal in favor of ThreadLocalRandom.
This commit is contained in:
		| @@ -16,35 +16,19 @@ | ||||
|  */ | ||||
| package org.l2jmobius.commons.util; | ||||
|  | ||||
| import java.util.Random; | ||||
| import java.util.concurrent.ThreadLocalRandom; | ||||
|  | ||||
| /** | ||||
|  * @author Mobius | ||||
|  */ | ||||
| public class Rnd | ||||
| { | ||||
| 	/** | ||||
| 	 * Thread-specific random number generator.<br> | ||||
| 	 * Each is seeded with the thread ID, so the sequence of random numbers are unique between threads. | ||||
| 	 */ | ||||
| 	// Java 1.8 | ||||
| 	// private static ThreadLocal<Random> RANDOM = new ThreadLocal<Random>() | ||||
| 	// Java 10 | ||||
| 	private static ThreadLocal<Random> RANDOM = new ThreadLocal<>() | ||||
| 	{ | ||||
| 		@Override | ||||
| 		protected Random initialValue() | ||||
| 		{ | ||||
| 			return new Random(System.nanoTime() + Thread.currentThread().getId()); | ||||
| 		} | ||||
| 	}; | ||||
| 	 | ||||
| 	/** | ||||
| 	 * @return a random boolean value. | ||||
| 	 */ | ||||
| 	public static boolean nextBoolean() | ||||
| 	{ | ||||
| 		return RANDOM.get().nextBoolean(); | ||||
| 		return ThreadLocalRandom.current().nextBoolean(); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -53,7 +37,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static void nextBytes(byte[] bytes) | ||||
| 	{ | ||||
| 		RANDOM.get().nextBytes(bytes); | ||||
| 		ThreadLocalRandom.current().nextBytes(bytes); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -62,7 +46,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static int get(int bound) | ||||
| 	{ | ||||
| 		return (int) (RANDOM.get().nextDouble() * bound); | ||||
| 		return ThreadLocalRandom.current().nextInt(bound); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -72,11 +56,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static int get(int origin, int bound) | ||||
| 	{ | ||||
| 		if (origin == bound) | ||||
| 		{ | ||||
| 			return origin; | ||||
| 		} | ||||
| 		return origin + (int) (((bound - origin) + 1) * RANDOM.get().nextDouble()); | ||||
| 		return ThreadLocalRandom.current().nextInt(origin, bound == Integer.MAX_VALUE ? bound : bound + 1); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -84,7 +64,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static int nextInt() | ||||
| 	{ | ||||
| 		return RANDOM.get().nextInt(); | ||||
| 		return ThreadLocalRandom.current().nextInt(); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -93,7 +73,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static long get(long bound) | ||||
| 	{ | ||||
| 		return (long) (RANDOM.get().nextDouble() * bound); | ||||
| 		return ThreadLocalRandom.current().nextLong(bound); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -103,11 +83,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static long get(long origin, long bound) | ||||
| 	{ | ||||
| 		if (origin == bound) | ||||
| 		{ | ||||
| 			return origin; | ||||
| 		} | ||||
| 		return origin + (long) (((bound - origin) + 1) * RANDOM.get().nextDouble()); | ||||
| 		return ThreadLocalRandom.current().nextLong(origin, bound == Long.MAX_VALUE ? bound : bound + 1L); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -115,7 +91,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static long nextLong() | ||||
| 	{ | ||||
| 		return RANDOM.get().nextLong(); | ||||
| 		return ThreadLocalRandom.current().nextLong(); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -124,7 +100,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static double get(double bound) | ||||
| 	{ | ||||
| 		return RANDOM.get().nextDouble() * bound; | ||||
| 		return ThreadLocalRandom.current().nextDouble(bound); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -138,7 +114,7 @@ public class Rnd | ||||
| 		{ | ||||
| 			return origin; | ||||
| 		} | ||||
| 		return origin + (((bound - origin) + 1) * RANDOM.get().nextDouble()); | ||||
| 		return ThreadLocalRandom.current().nextDouble(origin, bound == Double.MAX_VALUE ? bound : bound + 0.000000000000001d); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -146,7 +122,7 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static double nextDouble() | ||||
| 	{ | ||||
| 		return RANDOM.get().nextDouble(); | ||||
| 		return ThreadLocalRandom.current().nextDouble(); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| @@ -154,6 +130,6 @@ public class Rnd | ||||
| 	 */ | ||||
| 	public static double nextGaussian() | ||||
| 	{ | ||||
| 		return RANDOM.get().nextGaussian(); | ||||
| 		return ThreadLocalRandom.current().nextGaussian(); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 MobiusDevelopment
					MobiusDevelopment