package ieg.util.lang;

import java.util.Random;

public class MoreMath {

	/**
	 * randomly orders integers from <tt>0</tt> to <tt>n</tt> using Durstenfeld-Fisher-Yates shuffling.
	 * These can be used as unique random numbers. 
	 * @param n number of returned integers
	 * @return array of length <tt>n</tt>
	 */
	public static int[] generateRandomlyOrderedIntegers(int n) {
		// http://stackoverflow.com/questions/196017/unique-non-repeating-random-numbers-in-o1#196065
		// http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array#1520212
		
		int[] result = new int[n];
		for (int i = 0; i < n; i++) {
			result[i] = i;
		}
		// If running on Java 6 or older, use `new Random()` on RHS here
		// on Java 7++: java.util.concurrent.ThreadLocalRandom.current()
		Random rnd = new Random();
		for (int i = n - 1; i > 0; i--) {
			int index = rnd.nextInt(i + 1);
			// Simple swap
			int temp = result[index];
			result[index] = result[i];
			result[i] = temp;
		}
		return result;
	}

}

@@ -43,16 +43,93 @@
 andom;%0A%0A
+/**%0A * some additional mathematical utilities.%0A * @author Alexander Rind%0A */%0A
 public c
@@ -1071,10 +1071,577 @@
 lt;%0A%09%7D%0A%0A
+%09/**%0A%09 * create a random alpha-numerical string, which is suitable as pseudo-unique id%0A%09 * @param chars length of output%0A%09 * @return a random alpha-numerical string%0A%09 */%0A%09public static String getRandomAlphaNum(int chars) %7B%0A%09    // 4 digit number in base36 (alphanumerical)%0A%09    // 46656 = 1000x36 = 36%5E3%0A%09    // 1632960 = Z000x36 = 36%5E4 - 36%5E3 %0A%09    final int base = 36;%0A%09    int low = (int) Math.round(Math.pow(base, chars - 1));%0A%09    int extent = low * (base - 1);%0A%09    int code = (int) (Math.random() * extent) + low;%0A%09    return Integer.toString(code, base);%0A%09%7D%0A%0A
 %7D%0A

create random alpha-numerical string suitable as pseudo-unique id