RFR: 8294693: Add Collections.shuffle overload that accepts RandomGenerator interface [v2]

Stuart Marks smarks at openjdk.org
Tue Oct 11 02:00:19 UTC 2022


On Sat, 8 Oct 2022 15:35:14 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:

>> Java 17 added RandomGenerator interface. However, existing method Collections.shuffle accepts old java.util.Random class. While since Java 19, it's possible to use Random.from(RandomGenerator) wrapper, it would be more convenient to provide direct overload shuffle(List<?> list, RandomGenerator rnd).
>
> Tagir F. Valeev has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Remove Random -> ThreadLocalRandom change

test/jdk/java/util/Collections/Shuffle.java line 66:

> 64:         RandomGeneratorFactory<RandomGenerator> factory = RandomGeneratorFactory.getDefault();
> 65:         list.sort(null);
> 66:         Collections.shuffle(list, factory.create(1));

This assumes that the default factory will accept a seed value that initializes its state so that the pseudorandom sequence is repeatable. Not an unreasonable assumption, but `create(long)` essentially says that it can ignore the seed, and `getDefault` says the algorithm may change over time. On the other hand if something does change such that the pseudorandom sequence isn't repeatable, this test will most likely fail immediately. I was poking around for an explicit way to request some kind of PRNG that's guaranteed to be started in repeatable state, but I couldn't find one. Hmmm.

-------------

PR: https://git.openjdk.org/jdk/pull/10520


More information about the core-libs-dev mailing list