Utility
Class RandomNumber

java.lang.Object
  |
  +--Utility.RandomNumber
Direct Known Subclasses:
Beta, ChiSquare, Erlang, Exponential, Geometric, Normal, Poisson, Triangular, Uniform

public class RandomNumber
extends java.lang.Object

The RandomNumber class implements a random number generator. It behaves as a uniform distribution in [0,1) that gnerates doubles. It provides no support for directly generating random integers.
It is intended to be used a parent class for other random numbers.

Extending this class ...
When creating a subclass, be sure to use the sample01() function instead of the nextDouble() function. Both discrete and continuous distributions exist. The sampleInt() function is intended to be used with discrete distributions and the sampleDouble() function is intended to be used with continuous distributions. To make the class easier to use, it will not be considered an error to call sampleDouble() for a discrete distribution. The integer returned from sampleInt() can easily be cast to a double. But, in the case of calling sampleInt() for a continous distribution, a RuntimeException will be thrown. So when in doubt, use the sampleDouble() function.
Thus, users should pay attention to whether or not a distribution they are using is continuous or discrete. The RandomNumber class is treated as a continuous distribution - Uniform [0,1).

Tests Performed
1000 samples were generated and the means and variances were examined. Subjectively, they seemed correct. A goodness of fit test was performed with 100 samples and 10 intervals. It succeeded about 19/20 times.


Constructor Summary
RandomNumber()
           
 
Method Summary
 boolean goodnessOfFitTest(int sampleSize, double[] seperators, double[] expected)
          Run a chi squared Goodness of Fit Test.
 boolean goodnessOfFitTest(int sampleSize, double[] seperators, double[] expected, int estimatedParams)
          Let the user specify how many parameters had to be estimated.
static int randomMixer()
           
static void resetRandomMixer(int mixer)
          Reset the random_mixer to a value greater than 5, default value is 5
protected  double sample01()
          This function returns a uniform double in the interval [0,1).
 double sampleDouble()
          Draw a random sample.
 int sampleInt()
          Draw a random sample.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomNumber

public RandomNumber()
Method Detail

resetRandomMixer

public static void resetRandomMixer(int mixer)
Reset the random_mixer to a value greater than 5, default value is 5

Parameters:
mixer - Should be an integer >= 5, it denotes the number of times mix the random number seeds.

randomMixer

public static int randomMixer()

sampleDouble

public double sampleDouble()
Draw a random sample. This function should be overridden by all derived classes. The RandomNumber class returns a uniform double in the interval [0,1).


sampleInt

public int sampleInt()
Draw a random sample. This function should be overridden by all derived classes. The RandomNumber class throws a RuntimeException when this function is invoked, but this function should be used in derived classes that implement discrete distributions.


sample01

protected double sample01()
This function returns a uniform double in the interval [0,1).
Call this function from derived classes rather than using super.sampleDouble(). Even though both functions return the same type of result, calling sampleDouble() from derived classes' implmentations of sampleDouble() may result in some nasty infinite recursion.


goodnessOfFitTest

public boolean goodnessOfFitTest(int sampleSize,
                                 double[] seperators,
                                 double[] expected)
Run a chi squared Goodness of Fit Test. This involves subdividing the range of a random variable into intervals (also known as buckets) and then taking a sample from the distribution and recording how many samples fall into each bucket. To use this test, one must also know how many balls are expected to fall in each bucket. This statistic measures the deviation of the observed values from the expected values. At this point, the chi square statistic is compared against a tabulated value and the test either succeeds or fails.

In stats jargon, the null hypothesis for this test is that the given random number generator generates numbers according to some known distribution. (Ex I think my generator generates numbers are normal with mean 3.5 and variance 28.8.) The alternative hypothesis is that the null hypothesis is false.
This test is conducted at the level of significance 0.05.

This test will succeed if the generator creates numbers that appear to match those in the expected array and will fail otherwise. There is only a 5% the test will fail if the random sample does indeed come from the distribution given by the expected frequencies.

Parameters:
sampleSize - The number of samples to draw.
seperators - The upper bounds of the intervals. The last upper bound is always +infinity, (and thus not specified). The bounds are included in the intervals. Ex: seperators[] = {3,4,5} would indicate four intervals: (-inf,3], (3,4], (4,5], and (5,+inf).
expected - How many observations are expected to fall in each of the given intervals.
Throws:
java.lang.RuntimeException - Thrown if the length of the expected array is not one bigger than the length of the seperators array.

goodnessOfFitTest

public boolean goodnessOfFitTest(int sampleSize,
                                 double[] seperators,
                                 double[] expected,
                                 int estimatedParams)
Let the user specify how many parameters had to be estimated.

See Also:
goodnessOfFitTest(int,double,double).