Java Array Shuffle shuffle(T[] array, Random random)

Here you can find the source of shuffle(T[] array, Random random)

Description

shuffle

License

Apache License

Declaration

public static <T> void shuffle(T[] array, Random random) 

Method Source Code

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

import java.util.Random;

public class Main {
    public static <T> void shuffle(T[] array, Random random) {
        int count;
        if (array != null && (count = array.length) > 1) {
            for (int index = count - 1; index > 0; --index) {
                int newIndex = random.nextInt(index + 1);
                T temp = array[index];/*w  ww  .  j a v a2  s  .  c  o m*/
                array[index] = array[newIndex];
                array[newIndex] = temp;
            }
        }
    }

    public static void shuffle(double[] array, Random random) {
        int count;
        if (array != null && (count = array.length) > 1) {
            for (int index = count - 1; index > 0; --index) {
                int newIndex = random.nextInt(index + 1);
                double temp = array[index];
                array[index] = array[newIndex];
                array[newIndex] = temp;
            }
        }
    }

    public static void shuffle(int[] array, Random random) {
        int count;
        if (array != null && (count = array.length) > 1) {
            for (int index = count - 1; index > 0; --index) {
                int newIndex = random.nextInt(index + 1);
                int temp = array[index];
                array[index] = array[newIndex];
                array[newIndex] = temp;
            }
        }
    }

    public static void shuffle(long[] array, Random random) {
        int count;
        if (array != null && (count = array.length) > 1) {
            for (int index = count - 1; index > 0; --index) {
                int newIndex = random.nextInt(index + 1);
                long temp = array[index];
                array[index] = array[newIndex];
                array[newIndex] = temp;
            }
        }
    }
}

Related

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