Class FisherYates

java.lang.Object
org.javarosa.xform.parse.FisherYates

public class FisherYates
extends Object
This class implements the Fisher-Yates list shuffling algorithm.

Combined with the ParkMiller Random Number Generator implementation on this library, cross-platform reproducible results can be guaranteed.

Verified compatible libraries are:

Using other libraries won't guarantee reproducible results.

The main difference between this implementation and the native Collections.shuffle(java.util.List<?>) consist on the way this one selects which elements to swap on each iteration. See inlined comments for more information.

  • Constructor Summary

    Constructors 
    Constructor Description
    FisherYates()  
  • Method Summary

    Modifier and Type Method Description
    static <T> List<T> shuffle​(List<T> input)
    Shuffle the input list of elements using a Random Random Number Generator instance to decide which positions get swapped on each iteration.
    static <T> List<T> shuffle​(List<T> input, long seed)
    Shuffle the input list of elements using a ParkMiller Random Number Generator instance to decide which positions get swapped on each iteration.
    static <T> List<T> shuffle​(List<T> input, Random random)
    Shuffle the input list of elements using a Random Random Number Generator instance to decide which positions get swapped on each iteration.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • shuffle

      public static <T> List<T> shuffle​(List<T> input)
      Shuffle the input list of elements using a Random Random Number Generator instance to decide which positions get swapped on each iteration.
      Type Parameters:
      T - Type parameter of the input List of elements
      Parameters:
      input - List of elements to be shuffled
      Returns:
      A new List of elements, containing the same elements as in the input List, but in a different order.
    • shuffle

      public static <T> List<T> shuffle​(List<T> input, long seed)
      Shuffle the input list of elements using a ParkMiller Random Number Generator instance to decide which positions get swapped on each iteration.

      Use the same seed for the RNGs when reproducible results are required.

      Type Parameters:
      T - Type parameter of the input List of elements
      Parameters:
      input - List of elements to be shuffled
      seed - Long number to use as seed of the RNG
      Returns:
      A new List of elements, containing the same elements as in the input List, but in a different order.
    • shuffle

      public static <T> List<T> shuffle​(List<T> input, Random random)
      Shuffle the input list of elements using a Random Random Number Generator instance to decide which positions get swapped on each iteration.

      Use the same seed for the RNGs when reproducible results are required. Warning: It's recommended to use shuffle(List, long) instead, to ensure you are using a cross-platform RNG like ParkMiller.

      Type Parameters:
      T - Type parameter of the input List of elements
      Parameters:
      input - List of elements to be shuffled
      random - Random instance with the RNG to be used
      Returns:
      A new List of elements, containing the same elements as in the input List, but in a different order.