Class Random

java.lang.Object
io.sentry.util.Random
All Implemented Interfaces:
Serializable

@Internal public final class Random extends Object implements Serializable
A simplified replacement for Random, based on pcg-java that we use for sampling, which is much faster than SecureRandom. This is necessary so that some security tools do not flag our Random usage as potentially insecure.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a PcgRSFast instance seeded with with 2 longs generated by xorshift*.
    Random(long seed, long streamNumber)
    Create a random number generator with the given seed and stream number.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
    boolean
    nextBoolean(double probability)
     
    byte
     
    void
    nextBytes(byte[] b)
     
    char
     
    double
     
    double
    nextDouble(boolean includeZero, boolean includeOne)
     
    float
     
    float
    nextFloat(boolean includeZero, boolean includeOne)
     
    int
    Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
    int
    nextInt(int n)
    Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
    long
     
    long
    nextLong(long n)
     
    short
     
    void
    setSeed(long seed, long streamNumber)
    Sets the seed of this random number generator using .

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Random

      public Random()
      Create a PcgRSFast instance seeded with with 2 longs generated by xorshift*. The values chosen are very likely not used as seeds in any other non argument constructor of any of the classes provided in this library.
    • Random

      public Random(long seed, long streamNumber)
      Create a random number generator with the given seed and stream number. The seed defines the current state in which the rng is in and corresponds to seeds usually found in other RNG instances. RNGs with different seeds are able to catch up after they exhaust their period and produce the same numbers. (2^63).

      Different stream numbers alter the increment of the rng and ensure distinct state sequences

      Only generators with the same seed AND stream numbers will produce identical values

      Parameters:
      seed - used to compute the starting state of the RNG
      streamNumber - used to compute the increment for the lcg.
  • Method Details

    • setSeed

      public void setSeed(long seed, long streamNumber)
      Sets the seed of this random number generator using . The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed.

      Only generators with the same seed AND stream numbers will produce identical values

      Parameters:
      seed - used to compute the starting state of the RNG
      streamNumber - used to compute the increment for the lcg.
    • nextByte

      public byte nextByte()
    • nextBytes

      public void nextBytes(byte[] b)
    • nextChar

      public char nextChar()
    • nextShort

      public short nextShort()
    • nextInt

      public int nextInt()
      Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.
      Returns:
      the next pseudorandom, uniformly distributed int value from this random number generator's sequence
    • nextInt

      public int nextInt(int n)
      Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
      Parameters:
      n - the upper bound (exclusive). Must be positive.
      Returns:
      the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence
    • nextBoolean

      public boolean nextBoolean()
      Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. The general contract of nextBoolean is that one boolean value is pseudorandomly generated and returned. The values true and false are produced with (approximately) equal probability.
      Returns:
      the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence
    • nextBoolean

      public boolean nextBoolean(double probability)
    • nextLong

      public long nextLong()
    • nextLong

      public long nextLong(long n)
    • nextDouble

      public double nextDouble()
    • nextDouble

      public double nextDouble(boolean includeZero, boolean includeOne)
    • nextFloat

      public float nextFloat()
    • nextFloat

      public float nextFloat(boolean includeZero, boolean includeOne)