001    package org.jaga.util;
002    
003    import java.util.Random;
004    import org.jaga.definitions.*;
005    
006    
007    
008    /**
009     * TODO: Complete these comments.
010     *
011     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
012     *
013     * <p><u>Company:</u> University College London and JAGA.Org
014     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
015     * </p>
016     *
017     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
018     *    This program is free software; you can redistribute it and/or modify
019     *    it under the terms of the GNU General Public License as published by
020     *    the Free Software Foundation, ONLY if you include a note of the original
021     *    author(s) in any redistributed/modified copy.<br/>
022     *    This program is distributed in the hope that it will be useful,
023     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
024     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     *    GNU General Public License for more details.<br/>
026     *    You should have received a copy of the GNU General Public License
027     *    along with this program; if not, write to the Free Software
028     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *    or see http://www.gnu.org/licenses/gpl.html</p>
030     *
031     * @author Greg Paperin (greg@jaga.org)
032     *
033     * @version JAGA public release 1.0 beta
034     */
035    
036    public class DefaultRandomGenerator extends Random implements RandomGenerator {
037    
038            public DefaultRandomGenerator() {
039            }
040    
041            public DefaultRandomGenerator(long seed) {
042                    super(seed);
043            }
044    
045            public byte nextByte() {
046                    return (byte) nextDouble(Byte.MIN_VALUE, Byte.MAX_VALUE);
047            }
048    
049            public byte nextByte(byte minInclusive, byte maxExclusive) {
050                    return (byte) nextDouble(minInclusive, maxExclusive);
051            }
052    
053            public int nextInt(int minInclusive, int maxExclusive) {
054                    return (int) nextDouble(minInclusive, maxExclusive);
055            }
056    
057            public long nextLong(long minInclusive, long maxExclusive) {
058                    return (long) nextDouble(minInclusive, maxExclusive);
059            }
060    
061            public float nextFloat(float minInclusive, float maxExclusive) {
062                    return (float) nextDouble(minInclusive, maxExclusive);
063            }
064    
065            public double nextDouble(double minInclusive, double maxExclusive) {
066                    if (!(minInclusive < maxExclusive))
067                            throw new IllegalArgumentException("Required that minInclusive < maxExclusive (but "
068                                                                                               + minInclusive + " >= " + maxExclusive + ")");
069                    double rnd = nextDouble() - 0.5;
070                    // double range = maxExclusive - minInclusive;
071                    // return minInclusive + rnd * range;
072                    double halfRange = maxExclusive * 0.5 - minInclusive * 0.5;
073                    return minInclusive + halfRange + (2.0 * rnd) * halfRange;
074            }
075    
076            public double nextGaussian(double mean, double stdDev) {
077                    double rnd = nextGaussian();
078                    return mean + rnd * stdDev;
079            }
080    
081    }