Jump to content

Array shuffling


Recommended Posts

Posted (edited)

LAST UPDATE: MARCH 10, 2015

 

You could use Collections by converting the array to a List but that would be more expensive and wouldn't work for primitives.

 

The ArrayUtilities.swapEntriesByIndex(...) method can be found here: http://osbot.org/forum/topic/65570-some-array-utilities-for-your-convenience/

package org.bjornkrols.algorithms.sequence_permutation;

import java.util.Random;

import org.bjornkrols.utilities.array.ArrayUtilities;

/**
 * Generates a random permutation of a finite set in an unbiased manner.
 * 
 * These methods can be used both procedurally (by not using the return value) 
 * and as a factory (by calling it on a copy of an existing array for example).
 * 
 * @param set	        The set to be shuffled.
 * @return		The shuffled set.
 * @author 		Bjorn Krols (Botre)
 * @version		0.2
 * @since		March 5, 2015
 */

public final class KnuthShuffle {

	private static final Random RANDOM = new Random();
	
	private KnuthShuffle() { 
		// This class should never be instantiated.
		// Do not delete or make accessible.
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static boolean[] shuffle(boolean[] set) {
		int length = set.length;
		// For each index i of the array, starting at 1 (because i = 0 would systematically return j = 0).
		for (int i = 1; i < length; i++) {
			// Find j, a random index between 0 (inclusive) and i (inclusive).
			int j = RANDOM.nextInt(i + 1);
			// Swap the entries at indexes i and j.
			ArrayUtilities.swapEntriesByIndex(set, i, j);
		}
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static byte[] shuffle(byte[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static short[] shuffle(short[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static char[] shuffle(char[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static int[] shuffle(int[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static long[] shuffle(long[] set) {	
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static float[] shuffle(float[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static double[] shuffle(double[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
	/**
	 * @see	KnuthShuffle
	 */
	public static Object[] shuffle(Object[] set) {
		int length = set.length;
		for (int i = 1; i < length; i++) ArrayUtilities.swapEntriesByIndex(set, i, RANDOM.nextInt(i + 1));
		return set;
	}
	
}
Edited by Botre
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...