Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Array shuffling

Featured Replies

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

  • Author

sup with your @see KnuthShuffle plz l2 doc

 

They all do the same, not going to copy and paste the explanation 10 times every time I feel like changing / completing / detailing the description tongue.png

Edited by Botre

They all do the same, not going to copy and paste the explanation 10 times every time I feel like changing / completing / detailing the description tongue.png

fair enough xd

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.