// Class: RandNumGenerator // // Author: Alyce Brady // // This class is based on the College Board's RandNumGenerator // (see http://www.collegeboard.com/student/testing/ap/compsci_a/case.html), // as allowed by the GNU General Public License. // // License Information: // This class is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation. // // This class is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. package edu.kzoo.util; import java.util.Random; /** * Kalamazoo College Utility Classes:
* * The RandNumGenerator class provides a singleton * object for random number generation. RandNumGenerator * extends the java.util.Random class, so the object of this * class inherits all methods defined for the Random class. * Using this class, though, many different objects can share a single * source of random numbers. This eliminates the potential problem of * having multiple random number generators generating sequences of * numbers that are too similar. * *

* Example of how to use RandNumGenerator: *


 *       RandNumGenerator randNumGen = RandNumGenerator.getInstance();
 *       int anInt = randNumGen.nextInt(4);
 *       double aDouble = randNumGen.nextDouble();
 *    
* *

* The original RandNumGenerator class, which provided * a getInstance method but did not extend Random, was * copyright© 2002 College Entrance Examination Board * (www.collegeboard.com). * * @author Alyce Brady * @version 1 December 2003 **/ public class RandNumGenerator extends Random { // Class Variable: Only one generator is created by this class. private static RandNumGenerator theRandNumGenerator = new RandNumGenerator(); /* Private constructor ensures that a RandNumGenerator is ONLY * acquired through the getInstance method. */ private RandNumGenerator() { } /** Returns a random number generator. * Always returns the same RandNumGenerator object to * provide a better sequence of random numbers. **/ public static RandNumGenerator getInstance() { return theRandNumGenerator; } // Pretend to redefine key inherited Random methods so that they show // up as part of the RandNumGenerator class documentation. /** Sets the seed of this random number generator using a single * long seed. For more technical details, see the * class documentation for the Random class. * @return a pseudorandom, uniformly distributed boolean * value from this random number generator's sequence * @see java.util.Random#setSeed(long) **/ public void setSeed(long seed) { super.setSeed(seed); } /** Returns a pseudorandom, uniformly distributed boolean * value from this random number generator's sequence. The values * true and false are produced with * (approximately) equal probability. For more technical details, * see the class documentation for the Random class. * @return a pseudorandom, uniformly distributed boolean * value from this random number generator's sequence * @see java.util.Random#nextBoolean **/ public boolean nextBoolean() { return super.nextBoolean(); } /** Returns a pseudorandom, uniformly distributed double * value between 0.0 and 1.0 from this * random number generator's sequence. For more technical details, * see the class documentation for the Random class. * @return a pseudorandom, uniformly distributed double * value between 0.0 and 1.0 from * this random number generator's sequence. * @see java.util.Random#nextDouble **/ public double nextDouble() { return super.nextDouble(); } /** Returns a pseudorandom, uniformly distributed int value * between 0 (inclusive) and the specified value (exclusive), * drawn from this random number generator's sequence. For more * technical details, see the class documentation for the * Random class. * @param n the bound on the random number to be returned. * Must be positive. * @return a pseudorandom, uniformly distributed int * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException n is not positive * @see java.util.Random#nextInt(int) **/ public int nextInt(int n) { return super.nextInt(n); } }