/*
 * @(#) Random.java 2003/03/22
 */

/**
 * Random Number generator for GA. All methods in this class may become static.
 * 
 * @author Sushil J. Louis
 */
public class GARandom implements java.io.Serializable{

    static java.util.Random gaRandomizer;

    /**
     * Constructor must be passed a long seed for initializing the random
     * number generator.
     */

    public GARandom(long seed){
	gaRandomizer = new java.util.Random(seed);
    }

    /**
     * Takes a probability and returns a 1 with that probability.
     *
     * @param prob the probability with which to return a 1.
     *
     * @return a 0 or a 1 with probability prob.
     */
    public static int flip(double prob){
	if (prob == 1.0) {
	    return 1;
	}
	double rand = gaRandomizer.nextDouble();
	if(rand <= prob) {
	    return 1;
	} else {
	    return 0;
	}
    }

    /**
     * Returns the next pseudorandom, uniformly distributed double 
     * value between 0.0 and 1.0 from this random number generator's sequence. 
     *
     * @return The next pseudorandom, uniformly distributed double
     * value between 0.0 and 1.0 from this random number generator's sequence.
     */
    public static double fRandom(){
	return gaRandomizer.nextDouble();
    }
    
    /**
     * Returns a random int between low and high inclusive.
     *
     * @param low the lower (inclusive) limit of the random integer returned
     * @param high the upper (inclusive) limit of the random integer returned
     *
     * @return a random integer between low and high inclusive.
     */
    public static int rnd(int low, int high){
	if(high <= low) {
	    return low;
	}
	int diff = high - low;
	int rand = gaRandomizer.nextInt(diff);
	return low + rand;
    }

}
