Android examples for java.util:Random
Shuffling algorithm, Randomly permutes the specified array using a default source of randomness
//package com.java2s; import java.util.Random; public class Main { /**/*from ww w . j a v a2 s .c o m*/ * Shuffling algorithm, Randomly permutes the specified array using a default source of randomness */ public static boolean shuffle(Object[] objArray) { if (objArray == null) { return false; } return shuffle(objArray, getRandom(objArray.length)); } /** * Shuffling algorithm, Randomly permutes the specified array */ public static boolean shuffle(Object[] objArray, int shuffleCount) { int length; if (objArray == null || shuffleCount < 0 || (length = objArray.length) < shuffleCount) { return false; } for (int i = 1; i <= shuffleCount; i++) { int random = getRandom(length - i); Object temp = objArray[length - i]; objArray[length - i] = objArray[random]; objArray[random] = temp; } return true; } /** * Shuffling algorithm, Randomly permutes the specified int array using a default source of randomness */ public static int[] shuffle(int[] intArray) { if (intArray == null) { return null; } return shuffle(intArray, getRandom(intArray.length)); } /** * Shuffling algorithm, Randomly permutes the specified int array */ public static int[] shuffle(int[] intArray, int shuffleCount) { int length; if (intArray == null || shuffleCount < 0 || (length = intArray.length) < shuffleCount) { return null; } int[] out = new int[shuffleCount]; for (int i = 1; i <= shuffleCount; i++) { int random = getRandom(length - i); out[i - 1] = intArray[random]; int temp = intArray[length - i]; intArray[length - i] = intArray[random]; intArray[random] = temp; } return out; } /** * get a fixed-length random string, its a mixture of chars in source * * @param source * @param length * @return <ul> * <li>if source is null or empty, return null</li> * <li>else see {@link RandomUtil#getRandom(char[] sourceChar, int length)}</li> * </ul> */ public static String getRandom(String source, int length) { return source == null ? null : getRandom(source.toCharArray(), length); } /** * get a fixed-length random string, its a mixture of chars in sourceChar * * @param sourceChar * @param length * @return <ul> * <li>if sourceChar is null or empty, return null</li> * <li>if length less than 0, return null</li> * </ul> */ public static String getRandom(char[] sourceChar, int length) { if (sourceChar == null || sourceChar.length == 0 || length < 0) { return null; } StringBuilder str = new StringBuilder(length); Random random = new Random(); for (int i = 0; i < length; i++) { str.append(sourceChar[random.nextInt(sourceChar.length)]); } return str.toString(); } /** * get random int between 0 and max * * @param max * @return <ul> * <li>if max <= 0, return 0</li> * <li>else return random int between 0 and max</li> * </ul> */ public static int getRandom(int max) { return getRandom(0, max); } /** * get random int between min and max * * @param min * @param max * @return <ul> * <li>if min > max, return 0</li> * <li>if min == max, return min</li> * <li>else return random int between min and max</li> * </ul> */ public static int getRandom(int min, int max) { if (min > max) { return 0; } if (min == max) { return min; } return min + new Random().nextInt(max - min); } }