Class Randoms

java.lang.Object
io.jenetics.internal.math.Randoms

public final class Randoms extends Object
Some random helper functions.
Since:
1.4
Version:
7.0
  • Method Details

    • nextByte

      public static byte nextByte(RandomGenerator random)
    • nextChar

      public static char nextChar(RandomGenerator random)
    • nextShort

      public static short nextShort(RandomGenerator random)
    • nextASCIIString

      public static String nextASCIIString(int length, RandomGenerator random)
    • nextASCIIString

      public static String nextASCIIString(RandomGenerator random)
    • toFloat

      public static float toFloat(int a)
    • toFloat

      public static float toFloat(long a)
    • toDouble

      public static double toDouble(long a)
    • toDouble

      public static double toDouble(int a, int b)
    • toFloat2

      public static float toFloat2(int a)
    • toFloat2

      public static float toFloat2(long a)
    • toDouble2

      public static double toDouble2(long a)
    • toDouble2

      public static double toDouble2(int a, int b)
    • indexes

      public static IntStream indexes(RandomGenerator random, int start, int end, double p)
      Create an IntStream which creates random indexes within the given range and the index probability.
      Parameters:
      random - the random engine used for calculating the random indexes
      start - the start index (inclusively)
      end - the end index (exclusively)
      p - the index selection probability
      Returns:
      an new random index stream
      Throws:
      IllegalArgumentException - if p is not a valid probability.
      Since:
      3.0
    • indexes

      public static IntStream indexes(RandomGenerator random, int n, double p)
      Create an IntStream which creates random indexes within the given range and the index probability.
      Parameters:
      random - the random engine used for calculating the random indexes
      n - the end index (exclusively). The start index is zero.
      p - the index selection probability
      Returns:
      an new random index stream
      Throws:
      IllegalArgumentException - if p is not a valid probability.
      NullPointerException - if the given random engine is null.
      Since:
      3.0
    • seedBytes

      public static byte[] seedBytes(int length)
      Create a new seed byte array of the given length.
      Parameters:
      length - the length of the returned byte array.
      Returns:
      a new seed byte array of the given length
      Throws:
      NegativeArraySizeException - if the given length is smaller than zero.
      See Also:
    • seed

      public static byte[] seed(byte[] seed)
      Fills the given byte array with random bytes, created by successive calls of the seed() method.
      Parameters:
      seed - the byte array seed to fill with random bytes.
      Returns:
      the given byte array, for method chaining.
      Throws:
      NullPointerException - if the seed array is null.
      See Also:
    • seed

      public static long seed()
      Calculating a 64 bit seed value which can be used for initializing PRNGs. This method uses a combination of System.nanoTime() and new Object().hashCode() calls to create a reasonable safe seed value:
      public static long seed() { return seed(System.nanoTime()); }
      This method passes all the statistical tests of the dieharder test suite—executed on a linux machine with JDK version 1.7. Since there is no prove that this will the case for every Java version and OS, it is recommended to only use this method for seeding other PRNGs.
      Returns:
      the random seed value.
      See Also:
    • seed

      public static long seed(long base)
      Uses the given base value to create a reasonable safe seed value. This is done by combining it with values of new Object().hashCode():
      public static long seed(final long base) { final long objectHashSeed = ((long)(new Object().hashCode()) << 32) | new Object().hashCode(); long seed = base^objectHashSeed; seed ^= seed << 17; seed ^= seed >>> 31; seed ^= seed << 8; return seed; }
      Parameters:
      base - the base value of the seed to create
      Returns:
      the created seed value.