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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<T> elements
      Input elements ordered as a list.
      private java.util.Random rng
      Random instance used by this generator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean hasNext()
      java.util.Iterator<T> iterator()
      T next()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, remove
    • Field Detail

      • elements

        private java.util.List<T> elements
        Input elements ordered as a list. This list is being decreased in size as the elements are generated.
      • rng

        private java.util.Random rng
        Random instance used by this generator.
    • 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>