Java Random permute(Object[] arr, Random random)

Here you can find the source of permute(Object[] arr, Random random)

Description

Fischer-Yates shuffling algorithm for permuting the contents of a coordinate array.

License

Open Source License

Declaration

private static void permute(Object[] arr, Random random) 

Method Source Code


//package com.java2s;
/*/*from  www .  j a v  a 2s  . c  om*/
 * Nanoverse: a declarative agent-based modeling language for natural and
 * social science.
 *
 * Copyright (c) 2015 David Bruce Borenstein and Nanoverse, LLC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

import java.util.*;

public class Main {
    /**
     * Fischer-Yates shuffling algorithm for permuting the contents of
     * a coordinate array.
     */
    private static void permute(Object[] arr, Random random) {
        for (int i = arr.length - 1; i > 0; i--) {
            int j = random.nextInt(i + 1);
            swap(arr, i, j);
        }
    }

    private static void swap(Object[] arr, int i, int j) {
        Object temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }
}

Related

  1. getRandomServices(int maxSize)
  2. getRandomUUID()
  3. isRandomList(List list)
  4. medianHelper(List list, int k, Random r)
  5. multSample(Random rng, double[] vals, double normsum)
  6. rand()
  7. Rand()
  8. randAngle()
  9. randFloat(float min, float max)