Java Array Shuffle shuffleIntArray(int[] arr)

Here you can find the source of shuffleIntArray(int[] arr)

Description

shuffle Int Array

License

Open Source License

Declaration

public static void shuffleIntArray(int[] arr) 

Method Source Code

//package com.java2s;
/*//  w ww  .j a va 2  s  . c o m
 * Copyright (c) 2017, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

import java.util.*;

public class Main {
    private static Random rnd = new Random();

    public static void shuffleIntArray(int[] arr) {
        if (arr == null || arr.length == 0)
            throw new IllegalArgumentException(
                    "Array cannot be null or have zero length");

        for (int i = arr.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            int a = arr[index];
            arr[index] = arr[i];
            arr[i] = a;
        }
    }
}

Related

  1. shuffleArray(T[] arr)
  2. shuffled(List list, Random random)
  3. shuffledList(List list)
  4. shuffleInPlace(E[] elems, Random rand)
  5. shuffleInPlace(int[] toShuffle, Random random)
  6. shuffleIntArray(int[] array)
  7. shuffleList(List list)
  8. shuffleList(List list)
  9. shuffleSublist(List objects, int start, int end)