com.mindprod.common11
Class Shuffle

java.lang.Object
  extended by com.mindprod.common11.Shuffle

public class Shuffle
extends java.lang.Object

Shuffles an int[], much like Collections.shuffle.

Since:
2007-10-03
Version:
1.0 2007-10-03 - Created with IntelliJ IDEA.
Author:
Roedy Green, Canadian Mind Products

Constructor Summary
Shuffle()
           
 
Method Summary
static void main(java.lang.String[] args)
          test driver demonstrate use of shuffle.
static void shuffle(int[] toShuffle)
          Shuffle an array whene Collections.shuffle is not available or where you need high quality randomness.
static void shuffle(int[] toShuffle, java.util.Random wheel)
          This method uses the same technique as Collections.shuffle, but is somewhat simpler with direct array access.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shuffle

public Shuffle()
Method Detail

shuffle

public static void shuffle(int[] toShuffle)
Shuffle an array whene Collections.shuffle is not available or where you need high quality randomness. Works much like Collections.shuffle. Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.

The hedge "approximately" is used in the foregoing description because default source of randomness is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.

This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.

Parameters:
toShuffle - the array to be shuffled.

shuffle

public static void shuffle(int[] toShuffle,
                           java.util.Random wheel)
This method uses the same technique as Collections.shuffle, but is somewhat simpler with direct array access. Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair.

This implementation traverses the array backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.

Parameters:
toShuffle - the array to be shuffled.
wheel - the source of randomness to use to shuffle the list.

main

public static void main(java.lang.String[] args)
test driver demonstrate use of shuffle.

Parameters:
args - not used.