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

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

Description

shuffle

License

Apache License

Declaration

static public void shuffle(int[] array, Random rand) 

Method Source Code

//package com.java2s;
/**//from   w  w w.ja va  2s .c  o m
 * Copyright 2014, Emory University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

import java.util.Random;

public class Main {
    static public void shuffle(int[] array, Random rand) {
        shuffle(array, rand, array.length);
    }

    /** Calls {@link #shuffle(List, Random, int)}, where {@code lastIndex = list.size()}. */
    static public <T> void shuffle(List<T> list, Random rand) {
        shuffle(list, rand, list.size());
    }

    static public void shuffle(int[] array, Random rand, int lastIndex) {
        int i, j, size = lastIndex - 1;

        for (i = 0; i < size; i++) {
            j = rand.nextInt(size - i) + i + 1;
            swap(array, i, j);
        }
    }

    /**
     * A slightly modified version of Durstenfeld's shuffle algorithm.
     * @param lastIndex shuffle up to this index (exclusive, cannot be greater than the list of the list).
     */
    static public <T> void shuffle(List<T> list, Random rand, int lastIndex) {
        int i, j, size = lastIndex - 1;

        for (i = 0; i < size; i++) {
            j = rand.nextInt(size - i) + i + 1;
            swap(list, i, j);
        }
    }

    static public void swap(int[] array, int index0, int index1) {
        int tmp = array[index0];
        array[index0] = array[index1];
        array[index1] = tmp;
    }

    static public <T> void swap(List<T> list, int index0, int index1) {
        T tmp = list.get(index0);
        list.set(index0, list.get(index1));
        list.set(index1, tmp);
    }

    /** @return the index'th item if exists; otherwise, {@code null}. */
    static public <T> T get(List<T> list, int index) {
        return isRange(list, index) ? list.get(index) : null;
    }

    /** @return the index'th item if exists; otherwise, {@code null}. */
    static public <T> T get(T[] array, int index) {
        return isRange(array, index) ? array[index] : null;
    }

    static public <T> boolean isRange(List<T> list, int index) {
        return 0 <= index && index < list.size();
    }

    static public <T> boolean isRange(T[] array, int index) {
        return 0 <= index && index < array.length;
    }
}

Related

  1. shuffle(int[] array)
  2. shuffle(int[] array)
  3. shuffle(int[] array)
  4. shuffle(int[] array)
  5. shuffle(int[] array)
  6. shuffle(int[] array, Random rand)
  7. shuffle(int[] array, Random rnd)
  8. shuffle(int[] ary)
  9. shuffle(int[] input)