Class ElementsSequenceGenerator<T>

  • Type Parameters:
    T - element type
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Iterator<T>

    public class ElementsSequenceGenerator<T>
    extends java.lang.Object
    implements java.util.Iterator<T>, java.lang.Iterable<T>
    Generates elements from the input collection in random order.

    An element can be generated only once. After all elements have been generated, this generator halts. At every step, an element is generated uniformly at random, which means that every element has an equal probability to be generated. This implementation is based on the Fisher-Yates algorithm. The generator is unbiased meaning the every permutation is equally likely.

    Author:
    Timofey Chudakov
    • Constructor Detail

      • ElementsSequenceGenerator

        public ElementsSequenceGenerator​(java.util.Collection<T> elements)
        Constructs a new ElementsSequenceGenerator.
        Parameters:
        elements - a collection of elements to generate elements from.
      • ElementsSequenceGenerator

        public ElementsSequenceGenerator​(java.util.Collection<T> elements,
                                         long seed)
        Constructs a new ElementsSequenceGenerator using the specified seed. Two different generators with the same seed will produce identical sequences given that the same collection of elements is provided.
        Parameters:
        elements - a collection of elements to generate elements from.
        seed - a seed for the random number generator
      • ElementsSequenceGenerator

        public ElementsSequenceGenerator​(java.util.Collection<T> elements,
                                         java.util.Random rng)
        Constructs a new ElementsSequenceGenerator using the specified random number generator rng. Two different generators will produce identical sequences from a collection of elements given that the random number generator produces the same sequence of numbers.
        Parameters:
        elements - a collection of elements to generate elements from.
        rng - a random number generator
    • Method Detail

      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<T>
      • next

        public T next()
        Specified by:
        next in interface java.util.Iterator<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>