Java Array Shuffle shuffle(int[] a)

Here you can find the source of shuffle(int[] a)

Description

shuffle

License

Apache License

Declaration

public static void shuffle(int[] a) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {
    static final Random rand = new Random();

    public static void shuffle(int[] a) {
        // Fischer-Yates shuffle

        for (int i = a.length - 1; i > 0; i--) {
            int j = rand.nextInt(i + 1);
            swap(a, i, j);//from  ww w . j  a  v  a2 s  . co m
        }
    }

    public static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

Related

  1. shuffle(final T[] array)
  2. shuffle(final T[] array)
  3. shuffle(int a[])
  4. shuffle(int arr[])
  5. shuffle(int size, boolean reallyShuffle)
  6. shuffle(int[] arr)
  7. shuffle(int[] arr)
  8. shuffle(int[] array)
  9. shuffle(int[] array)