net.paulhertz.util
Class RandUtil

java.lang.Object
  extended by net.paulhertz.util.RandUtil

public class RandUtil
extends Object

Implements a few useful random number methods, maintains an internal random number generator. You will only need to instantiate this class if you want to maintain different random number generators (for example, initialized with different seeds).


Constructor Summary
RandUtil()
           
 
Method Summary
 double calcGauss(double mean, double variance)
          Returns a Gaussian variable using standard algorithm from Crandall, Pascal Applications for the Sciences
 double gauss()
          Returns a Gaussian variable using Java library call to Random.nextGaussian.
 double gauss(double mean, double variance)
          Returns a Gaussian variable using a Java library call to Random.nextGaussian.
 double quickGauss()
          returns a Gaussian variable using Peitgen and Saupe's algorithm from The Science of Fractal Images
 double quickGauss(double mean, double variance)
          returns a quick approximation to a Gaussian variable using Peitgen and Saupe's algorithm from The Science of Fractal Images
 Random randGenerator()
          Returns the internal random number generator.
 Object randomElement(ArrayList arr)
          Returns a random element from a raw ArrayList.
 double randomElement(double[] arr)
          Returns a random double from an array.
 float randomElement(float[] arr)
          Returns a random float from an array.
 int randomElement(int[] arr)
          Returns a random int from an array.
 double randomInRange(double low, double high)
          Returns a random double in a range from high to low, inclusive.
 float randomInRange(float low, float high)
          Returns a random float in a range from high to low, inclusive.
 int randomInRange(int low, int high)
          Returns a random int in a range from high to low, inclusive.
 void setRandGenerator(Random newRandGenerator)
          Sets the internal random number generator to the supplied instance of Random.
 void setRandSeed(long seed)
          Sets the seed value for the internal random number generator.
 void shuffle(ArrayList<Object> arr)
          Shuffles an ArrayList into random order.
 void shuffle(double[] doubleArray)
          Shuffles an array of doubles into random order.
 void shuffle(float[] floatArray)
          Shuffles an array of floats into random order.
 void shuffle(int[] intArray)
          Shuffles an array of integers into random order.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandUtil

public RandUtil()
Method Detail

randGenerator

public Random randGenerator()
Returns the internal random number generator.

Returns:
an instance of Random

setRandGenerator

public void setRandGenerator(Random newRandGenerator)
Sets the internal random number generator to the supplied instance of Random.

Parameters:
newRandGenerator - an instance of Random

setRandSeed

public void setRandSeed(long seed)
Sets the seed value for the internal random number generator.

Parameters:
seed -

shuffle

public void shuffle(int[] intArray)
Shuffles an array of integers into random order. Implements Richard Durstenfeld's version of the Fisher-Yates algorithm, popularized by Donald Knuth. see http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

Parameters:
intArray - an array of ints, changed on exit

shuffle

public void shuffle(float[] floatArray)
Shuffles an array of floats into random order.

Parameters:
floatArray - an array of floats, changed on exit

shuffle

public void shuffle(double[] doubleArray)
Shuffles an array of doubles into random order.

Parameters:
doubleArray - an array of doubles, changed on exit

shuffle

public void shuffle(ArrayList<Object> arr)
Shuffles an ArrayList into random order.

Parameters:
arr - an ArrayList, changed on exit

randomInRange

public int randomInRange(int low,
                         int high)
Returns a random int in a range from high to low, inclusive.

Parameters:
low - lower bound of range
high - upper bound of range
Returns:
a random integer from low to high, inclusive.

randomInRange

public float randomInRange(float low,
                           float high)
Returns a random float in a range from high to low, inclusive.

Parameters:
low - lower bound of range
high - upper bound of range
Returns:
a random integer from low to high, inclusive.

randomInRange

public double randomInRange(double low,
                            double high)
Returns a random double in a range from high to low, inclusive.

Parameters:
low - lower bound of range
high - upper bound of range
Returns:
a random integer from low to high, inclusive.

randomElement

public int randomElement(int[] arr)
Returns a random int from an array.

Parameters:
arr - an array of int
Returns:
a randomly selected value from the supplied array

randomElement

public float randomElement(float[] arr)
Returns a random float from an array.

Parameters:
arr - an array of float
Returns:
a randomly selected value from the supplied array

randomElement

public double randomElement(double[] arr)
Returns a random double from an array.

Parameters:
arr - an array of double
Returns:
a randomly selected value from the supplied array

randomElement

public Object randomElement(ArrayList arr)
Returns a random element from a raw ArrayList.

Parameters:
arr - and ArrayList of untyped objects.
Returns:
a randomly selected value from the supplied array

gauss

public double gauss()
Returns a Gaussian variable using Java library call to Random.nextGaussian.

Returns:
a Gaussian-distributed random number with mean 0.0 and variance 1.0

gauss

public double gauss(double mean,
                    double variance)
Returns a Gaussian variable using a Java library call to Random.nextGaussian.

Parameters:
mean -
variance -
Returns:
a Gaussian-distributed random number with mean mean and variance variance

calcGauss

public double calcGauss(double mean,
                        double variance)
Returns a Gaussian variable using standard algorithm from Crandall, Pascal Applications for the Sciences

Parameters:
mean -
variance -
Returns:
a Gaussian-distributed random number

quickGauss

public double quickGauss()
returns a Gaussian variable using Peitgen and Saupe's algorithm from The Science of Fractal Images

Returns:
a random number that approximates a Gaussian distribution

quickGauss

public double quickGauss(double mean,
                         double variance)
returns a quick approximation to a Gaussian variable using Peitgen and Saupe's algorithm from The Science of Fractal Images

Parameters:
mean - mean value of the distribution
variance - variance of the distribution
Returns:
a random number that over repeated calls approximates a Gaussian distribution


Processing library IgnoCodeLib by Paul Hertz. (C) 2013