Class ThreadLocalRandomWrapper

    • Field Detail

      • threadLocal

        protected transient ThreadLocal<BaseRandom> threadLocal
        Holds the delegate for each thread.
    • Constructor Detail

      • ThreadLocalRandomWrapper

        public ThreadLocalRandomWrapper​(java8.util.function.Supplier<? extends BaseRandom> initializer)
        Wraps the given Supplier. This ThreadLocalRandomWrapper will be serializable if the Supplier is serializable.
        Parameters:
        initializer - a supplier that will be called to provide the initial BaseRandom for each thread.
      • ThreadLocalRandomWrapper

        public ThreadLocalRandomWrapper​(int seedSize,
                                        SeedGenerator seedGenerator,
                                        java8.util.function.Function<byte[],​? extends BaseRandom> creator)
        Wraps a seed generator and a function that takes a seed byte array as input. This ThreadLocalRandomWrapper will be serializable if the Function is serializable.
        Parameters:
        seedSize - the size of seed arrays to generate.
        seedGenerator - The seed generation strategy that will provide the seed value for each thread's BaseRandom.
        creator - a Function that creates a BaseRandom from each seed. Probably a constructor reference.
    • Method Detail

      • wrapLegacy

        public static ThreadLocalRandomWrapper wrapLegacy​(java8.util.function.LongFunction<Random> legacyCreator,
                                                          SeedGenerator seedGenerator)
        Uses this class and RandomWrapper to decorate any implementation of Random that can be constructed from a long seed into a fully-concurrent one.
        Parameters:
        legacyCreator - a function that provides the Random that underlies the returned wrapper on each thread, taking a seed as input.
        seedGenerator - the seed generator whose output will be fed to legacyCreator.
        Returns:
        a ThreadLocalRandomWrapper decorating instances created by legacyCreator.
      • wrapLongCreatorAsByteArrayCreator

        protected static java8.util.function.Function<byte[],​BaseRandom> wrapLongCreatorAsByteArrayCreator​(java8.util.function.LongFunction<Random> legacyCreator)
      • withProbabilityInternal

        protected boolean withProbabilityInternal​(double probability)
        Description copied from class: BaseRandom
        Called by BaseRandom.withProbability(double) to generate a boolean with a specified probability of returning true, after checking that probability is strictly between 0 and 1.
        Overrides:
        withProbabilityInternal in class BaseRandom
        Parameters:
        probability - The probability (between 0 and 1 exclusive) of returning true.
        Returns:
        True with probability equal to the probability parameter; false otherwise.
      • withProbability

        public boolean withProbability​(double probability)
        Description copied from class: BaseRandom

        Returns true with the given probability, and records that only 1 bit of entropy is being spent.

        When probability <= 0, instantly returns false without recording any entropy spent. Likewise, instantly returns true when probability >= 1.

        Overrides:
        withProbability in class BaseRandom
        Parameters:
        probability - The probability of returning true.
        Returns:
        True with probability equal to the probability parameter; false otherwise.
      • nextLong

        public long nextLong()
        Description copied from class: BaseRandom
        Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. Unlike the inherited implementation in Random.nextLong(), ones in BetterRandom generally can be expected to return all 264 possible values.
        Specified by:
        nextLong in interface Java8CompatRandom
        Overrides:
        nextLong in class BaseRandom
      • nextLong

        public long nextLong​(long bound)
        Description copied from class: BaseRandom
        Returns a pseudorandom long value between zero (inclusive) and the specified bound (exclusive).
        Overrides:
        nextLong in class BaseRandom
        Parameters:
        bound - the upper bound (exclusive). Must be positive.
        Returns:
        a pseudorandom long value between zero (inclusive) and the bound (exclusive)
      • nextInt

        public int nextInt​(int origin,
                           int bound)
        Description copied from class: BaseRandom
        Returns a pseudorandom int value between the specified origin (inclusive) and the specified bound (exclusive).
        Overrides:
        nextInt in class BaseRandom
        Parameters:
        origin - the least value returned
        bound - the upper bound (exclusive)
        Returns:
        a pseudorandom int value between the origin (inclusive) and the bound (exclusive)
      • nextLong

        public long nextLong​(long origin,
                             long bound)
        Description copied from class: BaseRandom
        Returns a pseudorandom long value between the specified origin (inclusive) and the specified bound (exclusive). This implementation is adapted from the reference implementation of Random.longs(long, long) from JDK 8.
        Overrides:
        nextLong in class BaseRandom
        Parameters:
        origin - the least value returned
        bound - the upper bound (exclusive)
        Returns:
        a pseudorandom long value between the origin (inclusive) and the bound (exclusive)
      • nextBytes

        public void nextBytes​(byte[] bytes)
        Description copied from class: BaseRandom
        Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array. Reimplemented for entropy-counting purposes.
        Specified by:
        nextBytes in interface Java8CompatRandom
        Overrides:
        nextBytes in class RandomWrapper
      • nextLongNoEntropyDebit

        protected long nextLongNoEntropyDebit()
        Description copied from class: BaseRandom
        Returns the next random long, but does not debit entropy.
        Overrides:
        nextLongNoEntropyDebit in class RandomWrapper
        Returns:
        a pseudorandom long with all possible values equally likely.
      • nextDoubleNoEntropyDebit

        public double nextDoubleNoEntropyDebit()
        Description copied from class: BaseRandom
        Returns the next random double between 0.0 (inclusive) and 1.0 (exclusive), but does not debit entropy.
        Overrides:
        nextDoubleNoEntropyDebit in class RandomWrapper
        Returns:
        a pseudorandom double.
      • nextGaussian

        public double nextGaussian()
        Description copied from class: BaseRandom
        Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence. Unlike the one in Random, this implementation is lockless.
        Specified by:
        nextGaussian in interface Java8CompatRandom
        Overrides:
        nextGaussian in class RandomWrapper
      • setSeed

        public void setSeed​(long seed)
        Description copied from class: BaseRandom
        Sets the seed of this random number generator using a single long seed, if this implementation supports that. If it is capable of using 64 bits or less of seed data (i.e. if {@link #getNewSeedLength()} <= {@link Long#BYTES}), then this method shall replace the entire seed as Random.setSeed(long) does; otherwise, it shall either combine the input with the existing seed as SecureRandom.setSeed(long) does, or it shall generate a new seed using the DefaultSeedGenerator. The latter is a backward-compatibility measure and can be very slow.
        Specified by:
        setSeed in interface Java8CompatRandom
        Overrides:
        setSeed in class RandomWrapper
      • debitEntropy

        protected void debitEntropy​(long bits)
        Description copied from class: BaseRandom
        Record that entropy has been spent, and schedule a reseeding if this PRNG has now spent as much as it's been seeded with.
        Overrides:
        debitEntropy in class BaseRandom
        Parameters:
        bits - The number of bits of entropy spent.
      • getEntropyBits

        public long getEntropyBits()
        Description copied from interface: EntropyCountingRandom
        Returns an estimate of the current amount of entropy. Every time the PRNG is reseeded, the entropy count is set to the new seed's length; and every time it is used, it is decreased by the number of random bits in the output rounded up. The amount of entropy can go below zero, giving an indication of how far the entropy has been stretched. This estimate is a lower bound if the seed is perfectly random and is not being reused.
        Specified by:
        getEntropyBits in interface EntropyCountingRandom
        Overrides:
        getEntropyBits in class BaseRandom
        Returns:
        The current estimated amount of entropy.
      • getNewSeedLength

        public int getNewSeedLength()
        Description copied from interface: ByteArrayReseedableRandom
        Returns the preferred length of a new byte-array seed. "Preferred" is implementation-defined when multiple seed lengths are supported, but should probably usually mean the longest one, since the longer the seed, the more random the output.
        Specified by:
        getNewSeedLength in interface ByteArrayReseedableRandom
        Overrides:
        getNewSeedLength in class RandomWrapper
        Returns:
        The desired length of a new byte-array seed.