Random.nextLong(long bound)?

Dawid Weiss dawid.weiss at gmail.com
Thu Oct 19 06:44:38 UTC 2017


Hi Steven,

> We were hoping to have a single sequence shared between the library that takes Random and our
> own code that hopes to generate bounded longs.  We want such tight control because we are writing
> a repeatable tester and want to use the seed to be able to replay sequences that cause our
> system to fail.

A repeatable randomized tester is a noble idea; have you perhaps tried
the randomized
testing package (full disclosure -- I'm the author) [1]?

This is used in Solr/Lucene and other projects for running JUnit tests
with predictable
randomness (where it's possible to assert it, of course [4]). The
Random instance you get from the test's context should be
predictable and has an implementation of nextLong() with a long cycle
(as pointed out, it's not the long range,
it's the rnd generator's cycle that is a problem here).

> complex for us to just implement the naïve re-roll algorithm [ while(sample >= bound) sample = nextLong(); ]

There are some utilities that should be helpful here [2], but
Sebastiano Vigna (dsiutil package [3]) has Random
implementations with very long cycles too.

Dawid

[1] https://github.com/randomizedtesting/randomizedtesting

[2] https://github.com/randomizedtesting/randomizedtesting/blob/aeebebef5e8961205c60c2f4d5f4bf82b68cf0a8/randomized-runner/src/main/java/com/carrotsearch/randomizedtesting/generators/RandomNumbers.java

[3] http://dsiutils.di.unimi.it/

[4] https://berlinbuzzwords.de/sites/berlinbuzzwords.de/files/media/documents/dawidweiss-randomizedtesting-pub.pdf


More information about the core-libs-dev mailing list