Java Array Shuffle shuffleArray(int[] ar)

Here you can find the source of shuffleArray(int[] ar)

Description

shuffle Array

License

Apache License

Declaration

public static void shuffleArray(int[] ar) 

Method Source Code

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

import java.util.*;

public class Main {

    public static void shuffleArray(int[] ar) {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            int a = ar[index];
            ar[index] = ar[i];//from  w  w  w  . j a va 2 s.c om
            ar[i] = a;
        }
    }

    public static <T> void shuffleArray(T[] ar) {
        Random rnd = new Random();
        for (int i = ar.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            // Simple swap
            T a = ar[index];
            ar[index] = ar[i];
            ar[i] = a;
        }
    }
}

Related

  1. shuffle(T[] array, Random random)
  2. shuffle(T[] data)
  3. shuffleArray(double[] ar)
  4. shuffleArray(int arr[])
  5. shuffleArray(int[] a)
  6. shuffleArray(int[] ar)
  7. shuffleArray(int[] ar, Random rnd, int mx)
  8. shuffleArray(int[] arr)
  9. shuffleArray(int[] array, int length)