Here you can find the source of shuffle(T[] a, int from, int to, Random rnd)
Parameter | Description |
---|---|
a | the array to be shuffled. |
from | the initial index of the range to be shuffled, inclusive |
to | the final index of the range to be shuffled, exclusive. |
rnd | the source of randomness to use to shuffle the list. |
public static <T> T[] shuffle(T[] a, int from, int to, Random rnd)
//package com.java2s; /*//from www .j a va2s . c om * Copyright (c) 2008 Kasper Nielsen. * * 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.Random; public class Main { /** * Randomly permute the specified array from using the specified source of randomness. All permutations occur with * equal likelihood assuming that the source of randomness is fair. * <p> * This implementation traverses the array backwards, from the to index up to the from + 1 index, repeatedly * swapping a randomly selected element into the "current position". Elements are randomly selected from the portion * of the array that runs from the from index to the current position, inclusive. * <p> * This method runs in linear time. * * @param a * the array to be shuffled. * @param from * the initial index of the range to be shuffled, inclusive * @param to * the final index of the range to be shuffled, exclusive. * @param rnd * the source of randomness to use to shuffle the list. */ public static <T> T[] shuffle(T[] a, int from, int to, Random rnd) { for (int i = to; i > from + 1; i--) { swap(a, i - 1, rnd.nextInt(i - from) + from); } return a; } /** Swaps the two specified elements in the specified array. */ static void swap(Object[] a, int i, int j) { Object o = a[i]; a[i] = a[j]; a[j] = o; } }