package ProGAL.math; import java.util.Random; /** * A wrapper for static randomization functions. * @author rfonseca */ public class Randomization { private static java.util.Random rand = new java.util.Random(); public static Random getGenerator() { return rand; } /** * Return a uniform random number between (including) i1 and (not including) i2. * @param i1 lower bound of random number * @param i2 upper bound of random number */ public static int randBetween(int i1, int i2){ int max = Math.max(i1, i2); int min = Math.min(i1, i2); return (int)(rand.nextDouble()*(max-min)+min); } /** * Return a uniform random double between (including) d1 and (not including) d2 * @param d1 lower bound of random number * @param d2 upper bound of random number */ public static double randBetween(double d1, double d2){ double max = Math.max(d1, d2); double min = Math.min(d1, d2); return rand.nextDouble()*(max-min)+min; } /** * Generate a random permutation of integers between (including) 0 and (not including) max * @param max the length of the permutation */ public static int[] randomPermutation(int max){ int[] ret = new int[max]; for(int i=0;i1; i--){ int r = randBetween(0,i); Object tmp = arr[i-1]; arr[i-1] = arr[r]; arr[r] = tmp; } return arr; } }