Java Array Shuffle shuffle(Random rand, O[] array)

Here you can find the source of shuffle(Random rand, O[] array)

Description

shuffle

License

Open Source License

Declaration

public static <O> void shuffle(Random rand, O[] array) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;

public class Main {

    public static <O> void shuffle(Random rand, O[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int j = rand.nextInt(i + 1);
            O temp = array[i];/*from   w w  w.j  a  v  a  2  s  .  c o m*/
            array[i] = array[j];
            array[j] = temp;
        }
    }

    public static void shuffle(Random rand, short[] array) {
        for (int i = array.length - 1; i > 0; i--) {
            int j = rand.nextInt(i + 1);
            short temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    /**
     * Method to generate an int between the lower and upper bound
     * 
     * @param rand - pseudo-random number generator interface
     * @param low - lower bound value
     * @param high - upper bound value
     * @return an int between the lower and upper bound
     */
    public static int nextInt(Random rand, int low, int high) {
        if (high < low) {
            throw new IllegalArgumentException("high must be greater than low");
        }

        return rand.nextInt() * (high - low) + low;
    }
}

Related

  1. shuffle(List list, int nswaps)
  2. shuffle(Object[] a)
  3. shuffle(Object[] a, Random r)
  4. shuffle(Object[] array)
  5. shuffle(Object[] objs, Random random, int start, int len)
  6. shuffle(short... a)
  7. shuffle(String str, Random randObj)
  8. shuffle(T[] a, int from, int to, Random rnd)
  9. shuffle(T[] array)