Java tutorial
/* * Java Terrain and Stellar System Ports * * Copyright (C) 2006 Martin H. Smith based on work by original * authors. * * Released under the terms of the GNU General Public License * * This program 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; either version 2 * of the License, or (at your option) any later version. * * This program 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * Linking TerraJ statically or dynamically with other modules is making a * combined work based on TerraJ. Thus, the terms and conditions of the * GNU General Public License cover the whole combination. * * In addition, as a special exception, the copyright holders of TerraJ * give you permission to combine this program with free software programs * or libraries that are released under the GNU LGPL and with code included * in the standard release of JOGL, Java Getopt and FreeMarker under the BSD * license (or modified versions of such code, with unchanged license) and with * Apache Commons and Log4J libraries under the Apache license (or modified versions * of such code. You may copy and distribute such a system following the terms * of the GNU GPL for TerraJ and the licenses of the other code concerned, * provided that you include the source code of that other code when and as the * GNU GPL requires distribution of source code. * * Note that people who make modified versions of TerraJ are not obligated to grant * this special exception for their modified versions; it is their choice whether * to do so. The GNU General Public License gives permission to release a modified * version without this exception; this exception also makes it possible to release * a modified version which carries forward this exception. */ /* * NonRandomRandom.java * * Created on December 25, 2005, 5:18 PM * */ package com.alvermont.terraj.stargen.util; import java.util.Random; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A test random number generator that always returns a known sequence. This may * sound strange but its useful for testing the code and comparing it against C * code that otherwise uses a different random number generator. * * The random numbers generated by this class are derived from the value of PI. * * @author martin * @version $Id: NonRandomRandom.java,v 1.2 2006/07/06 06:59:43 martin Exp $ */ public class NonRandomRandom extends Random { /** Our logging object */ private static Log log = LogFactory.getLog(NonRandomRandom.class); /** Creates a new instance of NonRandomRandom */ public NonRandomRandom() { } /** The current random number generator value */ private double myDoubleValue = 0.000; /** The value to use to increment the random number */ private double myIncrementValue = Math.PI; /** * Returns the next pseudorandom, uniformly distributed * <code>double</code> value between <code>0.0</code> and * <code>1.0</code> from this random number generator's sequence. <p> * The general contract of <tt>nextDouble</tt> is that one * <tt>double</tt> value, chosen (approximately) uniformly from the * range <tt>0.0d</tt> (inclusive) to <tt>1.0d</tt> (exclusive), is * pseudorandomly generated and returned. All * 2<font size="-1"><sup>53</sup></font> possible <tt>float</tt> * values of the form <i>m x </i>2<font size="-1"><sup>-53</sup> * </font>, where <i>m</i> is a positive integer less than * 2<font size="-1"><sup>53</sup></font>, are produced with * (approximately) equal probability. The method <tt>nextDouble</tt> is * implemented by class <tt>Random</tt> as follows: * <blockquote><pre> * public double nextDouble() { * return (((long)next(26) << 27) + next(27)) * / (double)(1L << 53); * }</pre></blockquote><p> * The hedge "approximately" is used in the foregoing description only * because the <tt>next</tt> method is only approximately an unbiased * source of independently chosen bits. If it were a perfect source or * randomly chosen bits, then the algorithm shown would choose * <tt>double</tt> values from the stated range with perfect uniformity. * <p>[In early versions of Java, the result was incorrectly calculated as: * <blockquote><pre> * return (((long)next(27) << 27) + next(27)) * / (double)(1L << 54);</pre></blockquote> * This might seem to be equivalent, if not better, but in fact it * introduced a large nonuniformity because of the bias in the rounding * of floating-point numbers: it was three times as likely that the * low-order bit of the significand would be 0 than that it would be * 1! This nonuniformity probably doesn't matter much in practice, but * we strive for perfection.] * * @return the next pseudorandom, uniformly distributed * <code>double</code> value between <code>0.0</code> and * <code>1.0</code> from this random number generator's sequence. */ public double nextDouble() { final double value = this.myDoubleValue; ++callCount; this.myDoubleValue += this.myIncrementValue; if (this.myDoubleValue > 1.0) { this.myDoubleValue = this.myDoubleValue - Math.floor(this.myDoubleValue); } return value; } /** * Holds value of property callCount. */ private int callCount; /** * Getter for property callCount. * @return Value of property callCount. */ public int getCallCount() { return this.callCount; } }