import java.util.Random; /** A class used to create pseudorandom numbers */ public class RandomGen extends Object implements RandomInterface { private Random r; /** Create a new random number generator based on the current time. */ public RandomGen() { // initializes r based on the current time r = new Random(); } /** Create a new random number generator using seed as a seed. * A seed is a number used to generate a certain pattern of random * numbers. It is NOT the upper or lower bound, nor the expected * value. * @param seed the seed for the random number generator */ public RandomGen(long seed) { r = new java.util.Random(seed); } /** Select a number from an exponential distribution. * @param returns random number selected from exponential distribution * with expected value alpha. * @return a random real number */ public double nextExp (double alpha) { double x; while ((x = r.nextDouble()) == 0); //loop until x!=0 return (- alpha * Math.log(x)); } /** Select a number from a uniform distribution. * @post returns a random int uniformly distributed between low and * high inclusive. * @param low the minimum random integer to return * @param high the maximum random integer to return * @return a random integer between low and high */ public int nextUniformInt (int low, int high) { double x; while ((x = r.nextDouble()) == 1); //loop until x!=1; return (int) Math.floor(x * (high - low + 1) + low); } }