Class Randomness

java.lang.Object
org.elasticsearch.common.Randomness

public final class Randomness extends Object
Provides factory methods for producing reproducible sources of randomness. Reproducible sources of randomness contribute to reproducible tests. When running the Elasticsearch test suite, the test runner will establish a global random seed accessible via the system property "tests.seed". By seeding a random number generator with this global seed, we ensure that instances of Random produced with this class produce reproducible sources of randomness under when running under the Elasticsearch test suite. Alternatively, a reproducible source of randomness can be produced by providing a setting a reproducible seed. When running the Elasticsearch server process, non-reproducible sources of randomness are provided (unless a setting is provided for a module that exposes a seed setting (e.g., NodeEnvironment#NODE_ID_SEED_SETTING)).
  • Method Details

    • get

      public static Random get(Settings settings, Setting<Long> setting)
      Provides a reproducible source of randomness seeded by a long seed in the settings with the key setting.
      Parameters:
      settings - the settings containing the seed
      setting - the setting to access the seed
      Returns:
      a reproducible source of randomness
    • get

      public static Random get()
      Provides a source of randomness that is reproducible when running under the Elasticsearch test suite, and otherwise produces a non-reproducible source of randomness. Reproducible sources of randomness are created when the system property "tests.seed" is set and the security policy allows reading this system property. Otherwise, non-reproducible sources of randomness are created.
      Returns:
      a source of randomness
      Throws:
      IllegalStateException - if running tests but was not able to acquire an instance of Random from RandomizedContext or tests are running but tests.seed is not set
    • createSecure

      public static SecureRandom createSecure()
      Provides a secure source of randomness. This acts exactly similar to get(), but returning a new SecureRandom.
    • shuffle

      public static void shuffle(List<?> list)