edu.unm.cs.cs351.f10.tdrl.p2
Class XRandom

java.lang.Object
  extended by java.util.Random
      extended by edu.unm.cs.cs351.f10.tdrl.p2.XRandom
All Implemented Interfaces:
Serializable

public class XRandom
extends Random

A small extension of java.util.Random, providing a couple of handy methods Random is missing.

Version:
1.1
Author:
ackley
See Also:
Serialized Form

Field Summary
static long serialVersionUID
           
 
Constructor Summary
XRandom()
          Create an XRandom initialized with a randomly-chosen seed, so results will tend to differ from run to run even if nothing else changes.
XRandom(long seed)
          Create an XRandom initialized with the specified seed, so results will remain identical from run to run -- so long as the same seed is used, and all other random objects and methods (such as Math.random()) are scrupulously avoided.
 
Method Summary
 int drawFromCDF(double[] cdf)
          Draws an index from an arbitrary (discrete) CDF.
 int drawFromPMF(double[] pmf)
          Draws a value from a discrete probability mass function (PMF) and returns the corresponding outcome.
<T> T
drawFromPMF(Map<T,Double> pmf)
          Draws a value from a discrete probability mass function (PMF) and returns the corresponding outcome.
 double nextExponential(double mean)
          Draw a sample from an exponentially distributed random variable with the given mean value.
 int nextGeometric(double p)
          Draw a sample from a geometric distribution with parameter p.
 boolean nextProbability(double probability)
          Return true at random the specified fraction of the time.
 
Methods inherited from class java.util.Random
next, nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, setSeed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

public static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

XRandom

public XRandom()
Create an XRandom initialized with a randomly-chosen seed, so results will tend to differ from run to run even if nothing else changes.


XRandom

public XRandom(long seed)
Create an XRandom initialized with the specified seed, so results will remain identical from run to run -- so long as the same seed is used, and all other random objects and methods (such as Math.random()) are scrupulously avoided.

Parameters:
seed - the seed to initialize the generator with.
Method Detail

nextExponential

public double nextExponential(double mean)
Draw a sample from an exponentially distributed random variable with the given mean value.

Parameters:
mean - the specified average value of the produced numbers
Returns:
a sample drawn from exponentially distributed random variable with average value 'mean'
Throws:
IllegalArgumentException - if the mean is equal to zero

nextGeometric

public int nextGeometric(double p)
Draw a sample from a geometric distribution with parameter p. The mean of the resulting geometric distribution will be 1/p.

Parameters:
p - Parameter of the geometric distribution.
Returns:
Sample drawn from geometric distribution with parameter p.
Throws:
IllegalArgumentException - if p is not a probability (i.e., if p<0.0 || p>1.0).

nextProbability

public boolean nextProbability(double probability)
Return true at random the specified fraction of the time. Setting probability to 0.5 makes this method act the same as nextBoolean(). Setting it to 0.0 makes this method always return false, setting it to 1.0 makes it always return true.

Parameters:
probability - the chance of returning true
Returns:
true with the given probability, false with 1-probability
Throws:
IllegalArgumentException - if probability is less than 0.0 or greater than 1.0

drawFromCDF

public int drawFromCDF(double[] cdf)
Draws an index from an arbitrary (discrete) CDF. This selects one entry out of an array containing cumulative probabilities, and returns the index of the selected entry.

The contents of the array are assumed to be CDF values, sorted in increasing order. That is, it is required that:

 cdf[j]>=cdf[i]  for all j>i
 cdf[cdf.length-1]==1.0
 
If violations of these conditions are detected, an IllegalStateException is generated.

Parameters:
cdf - Array containing conditional distribution function data
Returns:
index of selected outcome
Throws:
IllegalStateException - if cdf appears to be non-normalized.

drawFromPMF

public <T> T drawFromPMF(Map<T,Double> pmf)
Draws a value from a discrete probability mass function (PMF) and returns the corresponding outcome. The PMF is required to be represented as a Map from the outcome space onto probabilities. It is required that:

If a violation of these conditions is detected, this throws a IllegalStateException.

Type Parameters:
T - Type of the outcome space
Parameters:
pmf - Map from outcome space (T) onto probabilities.
Returns:
Element drawn from the outcome space according to the pmf.
Throws:
IllegalStateException - if the pmf is not properly normalized.

drawFromPMF

public int drawFromPMF(double[] pmf)
Draws a value from a discrete probability mass function (PMF) and returns the corresponding outcome. The PMF is required to be represented as an array of probabilities, and the returned value is the index of the selected element. It is required that:

If a violation of these conditions is detected, this throws a IllegalStateException.

Parameters:
pmf - Array of probability values for each index
Returns:
Index drawn according to the pmf.
Throws:
IllegalStateException - if the pmf is not properly normalized.