From charlesreid1

Fisher Yates or Knuth shuffle

import java.util.*;

// How Not To Shuffle
// Knuth, or Fisher-Yates, Shuffle Algorithm
// http://www.i-programmer.info/programming/theory/2744-how-not-to-shuffle-the-kunth-fisher-yates-algorithm.html

public class FisherYates {
    public static void main(String[] args) {

        int n = 50;
        double[] z = new double[n];

        for(int i=0; i<n; i++) {
            z[i] = (i+1)*100;
        }

        System.out.println(Arrays.toString(z));
        fisherYates(z);
        System.out.println(Arrays.toString(z));
    }

    public static void fisherYates(double[] arr) {
        Random r = new Random();
        int n = arr.length;
        int i = 0;
        double t = 0.0;
        while(n>1) {
            // n == 1 corresponds to swapping next to last with last
            // n > 1 corresponds to having at least two possible choices
            n--;
            i = r.nextInt(n);
            t = arr[n];
            arr[n] = arr[i];
            arr[i] = t;
        }
    }
}

Flags