Java Array Shuffle shuffle(int[] ary)

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

Description

shuffle

License

Open Source License

Parameter

Parameter Description
ary a parameter

Declaration

public static int[] shuffle(int[] ary) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from  www. j  a  v a2s . c om*/
     * 
     * shuffle
     * @param ary
     * @return
     */
    public static int[] shuffle(int[] ary) {
        return shuffle(0, ary, System.currentTimeMillis());
    }

    /**
     * 
     * shuffle
     *
     * @param start start iundex, which is included. 
     * @param ary
     * @return
     */
    public static int[] shuffle(int start, int[] ary) {
        return shuffle(start, ary, System.currentTimeMillis());
    }

    /**
     * random shuffle inspired by libsvm code
     * @param ary
     * @param seed
     * @return
     */
    public static int[] shuffle(int[] ary, long seed) {
        return shuffle(0, ary, seed);
    }

    /**
     * random shuffle inspired by libsvm code
     * 
     * return new int[] instance.
     * 
     * @param start start index, it is processed.
     * @param ary
     * @param seed
     * @return
     */
    public static int[] shuffle(int start, int[] ary, long seed) {
        assert start >= 0 && start < ary.length;
        int len = ary.length;
        // random shuffle
        Random rand = new Random(seed);

        int[] retary = new int[len];
        System.arraycopy(ary, 0, retary, 0, len);
        for (int i = start; i < len; i++) {
            int j = i + (int) (rand.nextDouble() * (len - i));
            int _ = retary[i];
            retary[i] = retary[j];
            retary[j] = _;
        }
        return retary;
    }
}

Related

  1. shuffle(int[] array)
  2. shuffle(int[] array)
  3. shuffle(int[] array, Random rand)
  4. shuffle(int[] array, Random rand)
  5. shuffle(int[] array, Random rnd)
  6. shuffle(int[] input)
  7. shuffle(int[] list, Random rnd)
  8. Shuffle(int[] v)
  9. shuffle(Integer[] data)