org.jscience.measure.random
Class RandomElement

java.lang.Object
  extended by org.jscience.measure.random.RandomElement
All Implemented Interfaces:
java.lang.Cloneable, RandomGenerator
Direct Known Subclasses:
JavaGenerator, RandomSeedable, RandomShuffle, RandomSynchronized

public abstract class RandomElement
extends java.lang.Object
implements RandomGenerator, java.lang.Cloneable

RandomElement is an abstract class that encapsulates random number generators. To base a class on it, you must define the method nextDouble() as described below. It is also likely that you will want to define a constructor or another mechanism for seeding the the generator. The other classes defined in RandomElement add value to the numbers generated by nextDouble()

Source code is available.

See Also:
RandomJava, RandomShuffle

Constructor Summary
RandomElement()
           
 
Method Summary
 int choose(int hi)
           
 int choose(int lo, int hi)
           
 java.lang.Object clone()
          DOCUMENT ME!
 boolean coin()
           
 boolean coin(double p)
           
 double gaussian()
          gaussian() uses the Box-Muller algorithm to transform raw()'s into gaussian deviates.
 double gaussian(double sd)
           
 void nextDouble(double[] d)
          Fill an entire array with doubles.
 void nextDouble(double[] d, int n)
          Fill part or all of an array with doubles.
 double powlaw(double alpha, double cut)
          generate a power-law distribution with exponent alpha and lower cutoff cut
 double uniform(double lo, double hi)
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jscience.measure.random.RandomGenerator
nextDouble
 

Constructor Detail

RandomElement

public RandomElement()
Method Detail

nextDouble

public void nextDouble(double[] d,
                       int n)
Fill part or all of an array with doubles. The method defined here uses multiple calls to nextDouble() to fill the array. You can eliminate the overhead of multiple method calls by subclassing this with a version of the generator that fills the array. On our system this improves the efficiency of Ranecu by 20% when filling large arrays.

Parameters:
d - array to be filled with doubles
n - number of doubles to generate

nextDouble

public void nextDouble(double[] d)
Fill an entire array with doubles. This method calls raw(double d[],int n) with d=d.length. Since this adds little overhead for d.length large, it is only necessary to override raw(double d[],int n)

Parameters:
d - array to be filled with doubles.

choose

public int choose(int hi)
Parameters:
hi - upper limit of range
Returns:
a random integer in the range 1,2,... ,hi

choose

public int choose(int lo,
                  int hi)
Parameters:
lo - lower limit of range
hi - upper limit of range
Returns:
a random integer in the range lo, lo+1, ... ,hi

coin

public boolean coin()
Returns:
a boolean that's true 0.5 of the time; equivalent to coin(0.5).

coin

public boolean coin(double p)
Parameters:
p - probability that function will return true
Returns:
a boolean that's true p of the time.

uniform

public double uniform(double lo,
                      double hi)
Parameters:
lo - lower limit of range
hi - upper limit of range
Returns:
a uniform random real in the range [lo,hi]

gaussian

public double gaussian()
gaussian() uses the Box-Muller algorithm to transform raw()'s into gaussian deviates.

Returns:
a random real with a gaussian distribution, standard deviation

gaussian

public double gaussian(double sd)
Parameters:
sd - standard deviation
Returns:
a gaussian distributed random real with standard deviation sd

powlaw

public double powlaw(double alpha,
                     double cut)
generate a power-law distribution with exponent alpha and lower cutoff cut

Parameters:
alpha - the exponent
cut - the lower cutoff

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
DOCUMENT ME!

Overrides:
clone in class java.lang.Object
Returns:
DOCUMENT ME!
Throws:
java.lang.CloneNotSupportedException - DOCUMENT ME!