From 2602439a51f109e059123b232ec60c9d816c60dd Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sat, 12 Dec 2015 17:51:46 -0500 Subject: [PATCH] Delegate to j.u.c.ThreadLocalRandom for Random instances --- .../org/elasticsearch/common/Randomness.java | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/Randomness.java b/core/src/main/java/org/elasticsearch/common/Randomness.java index e912a9977f1..dbfa8034b99 100644 --- a/core/src/main/java/org/elasticsearch/common/Randomness.java +++ b/core/src/main/java/org/elasticsearch/common/Randomness.java @@ -22,10 +22,10 @@ package org.elasticsearch.common; import org.elasticsearch.common.settings.Settings; import java.lang.reflect.Method; -import java.security.SecureRandom; import java.util.Collections; import java.util.List; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /** * Provides factory methods for producing reproducible sources of @@ -111,25 +111,10 @@ public final class Randomness { private static Random getWithoutSeed() { assert currentMethod == null && getRandomMethod == null : "running under tests but tried to create non-reproducible random"; - return RandomnessHelper.LOCAL.get(); + return ThreadLocalRandom.current(); } public static void shuffle(List list) { Collections.shuffle(list, get()); } - - private static class RandomnessHelper { - private static final SecureRandom SR = new SecureRandom(); - - private static final ThreadLocal LOCAL = ThreadLocal.withInitial( - () -> { - byte[] bytes = SR.generateSeed(8); - long accumulator = 0; - for (int i = 0; i < bytes.length; i++) { - accumulator = (accumulator << 8) + bytes[i] & 0xFFL; - } - return new Random(accumulator); - } - ); - } }