Class RandomRegistry


  • public final class RandomRegistry
    extends Object
    This class holds the Random engine used for the GA. The RandomRegistry is thread safe. The registry is initialized with the ThreadLocalRandom PRNG, which has a much better performance behavior than an instance of the Random class. Alternatively, you can initialize the registry with one of the PRNG, which are being part of the library.

    Setup of a global PRNG

    public class GA { public static void main(final String[] args) { // Initialize the registry with a ThreadLocal instance of the PRGN. // This is the preferred way setting a new PRGN. RandomRegistry.random(new LCG64ShiftRandom.ThreadLocal()); // Using a thread safe variant of the PRGN. Leads to slower PRN // generation, but gives you the possibility to set a PRNG seed. RandomRegistry.random(new LCG64ShiftRandom.ThreadSafe(1234)); ... final EvolutionResult<DoubleGene, Double> result = stream .limit(100) .collect(toBestEvolutionResult()); } }

    Setup of a local PRNG
    You can temporarily (and locally) change the implementation of the PRNG. E.g. for initialize the engine stream with the same initial population.

    public class GA { public static void main(final String[] args) { // Create a reproducible list of genotypes. final List<Genotype<DoubleGene>> genotypes = with(new LCG64ShiftRandom(123), r -> Genotype.of(DoubleChromosome.of(0, 10)).instances() .limit(50) .collect(toList()) ); final Engine<DoubleGene, Double> engine = ...; final EvolutionResult<DoubleGene, Double> result = engine // Initialize the evolution stream with the given genotypes. .stream(genotypes) .limit(100) .collect(toBestEvolutionResult()); } }

    Since:
    1.0
    Version:
    3.0
    See Also:
    Random, ThreadLocalRandom
    • Method Detail

      • random

        public static void random​(Random random)
        Set the new global Random object for the GA. The given Random must be thread safe, which is the case for the default Java Random implementation.

        Setting a thread-local random object leads, in general, to a faster PRN generation, because the given Random engine don't have to be thread-safe.

        Parameters:
        random - the new global Random object for the GA.
        Throws:
        NullPointerException - if the random object is null.
        See Also:
        random(ThreadLocal)
      • random

        public static void random​(ThreadLocal<? extends Random> random)
        Set the new global Random object for the GA. The given Random don't have be thread safe, because the given ThreadLocal wrapper guarantees thread safety. Setting a thread-local random object leads, in general, to a faster PRN generation, when using a non-blocking PRNG. This is the preferred way for changing the PRNG.
        Parameters:
        random - the thread-local random engine to use.
        Throws:
        NullPointerException - if the random object is null.
      • reset

        public static void reset()
        Set the random object to it's default value. The default used PRNG is the ThreadLocalRandom PRNG.
      • using

        public static <R extends Random> void using​(R random,
                                                    Consumer<? super R> consumer)
        Executes the consumer code using the given random engine.
        final MSeq<Integer> seq = ... using(new Random(123), r -> { seq.shuffle(); });
        The example above shuffles the given integer seq using the given Random(123) engine.
        Type Parameters:
        R - the type of the random engine
        Parameters:
        random - the PRNG used within the consumer
        consumer - the consumer which is executed with the scope of the given random engine.
        Throws:
        NullPointerException - if one of the arguments is null
        Since:
        3.0
      • using

        public static <R extends Random> void using​(ThreadLocal<R> random,
                                                    Consumer<? super R> consumer)
        Executes the consumer code using the given random engine.
        final MSeq<Integer> seq = ... using(new LCG64ShiftRandom.ThreadLocal(), r -> { seq.shuffle(); });
        The example above shuffles the given integer seq using the given LCG64ShiftRandom.ThreadLocal() engine.
        Type Parameters:
        R - the type of the random engine
        Parameters:
        random - the PRNG used within the consumer
        consumer - the consumer which is executed with the scope of the given random engine.
        Throws:
        NullPointerException - if one of the arguments is null
        Since:
        3.0
      • with

        public static <R extends Random,​T> T with​(R random,
                                                        Function<? super R,​? extends T> function)
        Opens a new Scope with the given random engine and executes the given function within it. The following example shows how to create a reproducible list of genotypes:
        final List<Genotype<DoubleGene>> genotypes = with(new LCG64ShiftRandom(123), r -> Genotype.of(DoubleChromosome.of(0, 10)).instances() .limit(50) .collect(toList()) );
        Type Parameters:
        R - the type of the random engine
        T - the function return type
        Parameters:
        random - the PRNG used for the opened scope
        function - the function to apply within the random scope
        Returns:
        the object returned by the given function
        Throws:
        NullPointerException - if one of the arguments is null
        Since:
        3.0
      • with

        public static <R extends Random,​T> T with​(ThreadLocal<R> random,
                                                        Function<? super R,​? extends T> function)
        Opens a new Scope with the given random engine and executes the given function within it. The following example shows how to create a reproducible list of genotypes:
        final List<Genotype<DoubleGene>> genotypes = with(new LCG64ShiftRandom.ThreadLocal(), random -> Genotype.of(DoubleChromosome.of(0, 10)).instances() .limit(50) .collect(toList()) );
        Type Parameters:
        R - the type of the random engine
        T - the function return type
        Parameters:
        random - the PRNG used for the opened scope
        function - the function to apply within the random scope
        Returns:
        the object returned by the given function
        Throws:
        NullPointerException - if one of the arguments is null.
        Since:
        3.0