Class Randomness


  • public final class Randomness
    extends java.lang.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 Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.security.SecureRandom createSecure()
      Provides a secure source of randomness.
      static java.util.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.
      static java.util.Random get​(Settings settings, Setting<java.lang.Long> setting)
      Provides a reproducible source of randomness seeded by a long seed in the settings with the key setting.
      static void shuffle​(java.util.List<?> list)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        public static java.util.Random get​(Settings settings,
                                           Setting<java.lang.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 java.util.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:
        java.lang.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 java.security.SecureRandom createSecure()
        Provides a secure source of randomness. This acts exactly similar to get(), but returning a new SecureRandom.
      • shuffle

        public static void shuffle​(java.util.List<?> list)