Here you can find the source of randomize(final T[] array)
Parameter | Description |
---|---|
array | a parameter |
public static <T> T[] randomize(final T[] array)
//package com.java2s; /*//from ww w . j ava2s.com * Copyright (C) 2011-2014 Brian Groenke * All rights reserved. * * This file is part of the 2DX Graphics Library. * * This Source Code Form is subject to the terms of the * Mozilla Public License, v. 2.0. If a copy of the MPL * was not distributed with this file, You can obtain one at * http://mozilla.org/MPL/2.0/. */ import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { /** * Reassigns all of the existing values in the given array to different * (pseudo-random) slots. * * @param array * @return the randomized array. */ public static <T> T[] randomize(final T[] array) { if (array == null) { throw (new NullPointerException("passed array is of null value")); } T[] copy = Arrays.copyOf(array, array.length); List<Integer> track = new ArrayList<Integer>(); for (int i = 0; i < array.length; i++) { int rand = -1; do { rand = (int) (Math.random() * array.length); } while (track.contains(rand)); array[i] = copy[rand]; track.add(rand); } return array; } }