Package edu.cornell.lassp.houle.RngPack
Class RandomElement
- java.lang.Object
-
- edu.cornell.lassp.houle.RngPack.RandomElement
-
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
RandomSeedable
public abstract class RandomElement extends Object implements Cloneable
RandomElement is an abstract class that encapsulates random number generators. To base a class on it, you must define the methodraw()
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 inRandomElement
add value to the numbers generated byraw()
Source code is available.
- Version:
- 1.1a
- Author:
- Paul Houle (E-mail: [email protected])
-
-
Constructor Summary
Constructors Constructor Description RandomElement()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
choose(int hi)
int
choose(int lo, int hi)
Object
clone()
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)
double
powlaw(double alpha, double cut)
generate a power-law distribution with exponentalpha
and lower cutoffcut
abstract double
raw()
The abstract method that must be defined to make a working RandomElement.void
raw(double[] d)
Fill an entire array with doubles.void
raw(double[] d, int n)
Fill part or all of an array with doubles.double
uniform(double lo, double hi)
-
-
-
Method Detail
-
raw
public abstract double raw()
The abstract method that must be defined to make a working RandomElement. See the classRandomJava
for an example of how to do this.- Returns:
- a random double in the range [0,1]
- See Also:
RandomJava
-
raw
public void raw(double[] d, int n)
Fill part or all of an array with doubles. The method defined here uses multiple calls toraw()
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 ofRanecu
by 20% when filling large arrays.- Parameters:
d
- array to be filled with doublesn
- number of doubles to generate
-
raw
public void raw(double[] d)
Fill an entire array with doubles. This method callsraw(double d[],int n)
withd=d.length
. Since this adds little overhead ford.length
large, it is only necessary to overrideraw(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 rangehi
- 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 rangehi
- 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 exponentalpha
and lower cutoffcut
- Parameters:
alpha
- the exponentcut
- the lower cutoff
-
clone
public Object clone() throws CloneNotSupportedException
- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
-
-