## org.apache.commons.math.random
Class ValueServer

    java.lang.Object
      org.apache.commons.math.random.ValueServer

    public class ValueServer

extends java.lang.Object

Generates values for use in simulation applications.

How values are generated is determined by the `mode` property.

Supported `mode` values are:

- DIGEST_MODE -- uses an empirical distribution
- REPLAY_MODE -- replays data from `valuesFileURL`
- UNIFORM_MODE -- generates uniformly distributed random values with mean = `mu`
- EXPONENTIAL_MODE -- generates exponentially distributed random values with mean = `mu`
- GAUSSIAN_MODE -- generates Gaussian distributed random values with mean = `mu` and standard deviation = `sigma`
- CONSTANT_MODE -- returns `mu` every time.

Version:
  $Revision: 811827 $ $Date: 2009-09-06 11:32:50 -0400 (Sun, 06 Sep 2009) $

Field Summary
`static int` `CONSTANT_MODE` Always return mu
`static int` `DIGEST_MODE` Use empirical distribution.
`private EmpiricalDistribution` `empiricalDistribution` Empirical probability distribution for use with DIGEST_MODE.
`static int` `EXPONENTIAL_MODE` Exponential random deviates with mean = μ.
`private java.io.BufferedReader` `filePointer` File pointer for REPLAY_MODE.
`static int` `GAUSSIAN_MODE` Gaussian random deviates with mean = μ, std dev = σ.
`private int` `mode` mode determines how values are generated.
`private double` `mu` Mean for use with non-data-driven modes.
`private RandomData` `randomData` RandomDataImpl to use for random data generation.
`static int` `REPLAY_MODE` Replay data from valuesFilePath.
`private double` `sigma` Standard deviation for use with GAUSSIAN_MODE.
`static int` `UNIFORM_MODE` Uniform random deviates with mean = μ.
`private java.net.URL` `valuesFileURL` URI to raw data values.

Constructor Summary
`ValueServer()` Creates new ValueServer
`ValueServer(RandomData randomData)` Construct a ValueServer instance using a RandomData as its source of random data.

Method Summary
`void` `closeReplayFile()` Closes `valuesFileURL` after use in REPLAY_MODE.
`void` `computeDistribution()` Computes the empirical distribution using values from the file in `valuesFileURL`, using the default number of bins.
`void` `computeDistribution(int binCount)` Computes the empirical distribution using values from the file in `valuesFileURL` and `binCount` bins.
`void` `fill(double[] values)` Fills the input array with values generated using getNext() repeatedly.
`double[]` `fill(int length)` Returns an array of length `length` with values generated using getNext() repeatedly.
`EmpiricalDistribution` `getEmpiricalDistribution()` Getter for property empiricalDistribution.
`int` `getMode()` Getter for property mode.
`double` `getMu()` Getter for property mu.
`double` `getNext()` Returns the next generated value, generated according to the mode value (see MODE constants).
`private double` `getNextDigest()` Gets a random value in DIGEST_MODE.
`private double` `getNextExponential()` Gets an exponentially distributed random value with mean = mu.
`private double` `getNextGaussian()` Gets a Gaussian distributed random value with mean = mu and standard deviation = sigma.
`private double` `getNextReplay()` Gets next sequential value from the `valuesFileURL`.
`private double` `getNextUniform()` Gets a uniformly distributed random value with mean = mu.
`double` `getSigma()` Getter for property sigma.
`java.net.URL` `getValuesFileURL()` Getter for `valuesFileURL```
`void` `resetReplayFile()` Resets REPLAY_MODE file pointer to the beginning of the `valuesFileURL`.
`void` `setMode(int mode)` Setter for property mode.
`void` `setMu(double mu)` Setter for property mu.
`void` `setSigma(double sigma)` Setter for property sigma.
`void` `setValuesFileURL(java.lang.String url)` Sets the `valuesFileURL` using a string URL representation
`void` `setValuesFileURL(java.net.URL url)` Sets the `valuesFileURL`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DIGEST_MODE

    public static final int DIGEST_MODE

  Use empirical distribution.

  See Also:
    Constant Field Values

### REPLAY_MODE

    public static final int REPLAY_MODE

  Replay data from valuesFilePath.

  See Also:
    Constant Field Values

### UNIFORM_MODE

    public static final int UNIFORM_MODE

  Uniform random deviates with mean = μ.

  See Also:
    Constant Field Values

### EXPONENTIAL_MODE

    public static final int EXPONENTIAL_MODE

  Exponential random deviates with mean = μ.

  See Also:
    Constant Field Values

### GAUSSIAN_MODE

    public static final int GAUSSIAN_MODE

  Gaussian random deviates with mean = μ, std dev = σ.

  See Also:
    Constant Field Values

### CONSTANT_MODE

    public static final int CONSTANT_MODE

  Always return mu

  See Also:
    Constant Field Values

### mode

    private int mode

  mode determines how values are generated.

### valuesFileURL

    private java.net.URL valuesFileURL

  URI to raw data values.

### mu

    private double mu

  Mean for use with non-data-driven modes.

### sigma

    private double sigma

  Standard deviation for use with GAUSSIAN_MODE.

### empiricalDistribution

    private EmpiricalDistribution empiricalDistribution

  Empirical probability distribution for use with DIGEST_MODE.

### filePointer

    private java.io.BufferedReader filePointer

  File pointer for REPLAY_MODE.

### randomData

    private RandomData randomData

  RandomDataImpl to use for random data generation.

Constructor Detail

### ValueServer

    public ValueServer()

  Creates new ValueServer

### ValueServer

    public ValueServer(RandomData randomData)

  Construct a ValueServer instance using a RandomData as its source of random data.

  Parameters:
    `randomData` - the RandomData instance used to source random data
  Since:
    1.1

Method Detail

### getNext

    public double getNext()
                   throws java.io.IOException

  Returns the next generated value, generated according to the mode value (see MODE constants).

  Returns:
    generated value
  Throws:
    `java.io.IOException` - in REPLAY_MODE if a file I/O error occurs

### fill

    public void fill(double[] values)
              throws java.io.IOException

  Fills the input array with values generated using getNext() repeatedly.

  Parameters:
    `values` - array to be filled
  Throws:
    `java.io.IOException` - in REPLAY_MODE if a file I/O error occurs

### fill

    public double[] fill(int length)
                  throws java.io.IOException

  Returns an array of length `length` with values generated using getNext() repeatedly.

  Parameters:
    `length` - length of output array
  Returns:
    array of generated values
  Throws:
    `java.io.IOException` - in REPLAY_MODE if a file I/O error occurs

### computeDistribution

    public void computeDistribution()
                             throws java.io.IOException

  Computes the empirical distribution using values from the file in `valuesFileURL`, using the default number of bins.

  `valuesFileURL` must exist and be readable by *this at runtime.

  This method must be called before using `getNext()` with `mode = DIGEST_MODE`

  Throws:
    `java.io.IOException` - if an I/O error occurs reading the input file

### computeDistribution

    public void computeDistribution(int binCount)
                             throws java.io.IOException

  Computes the empirical distribution using values from the file in `valuesFileURL` and `binCount` bins.

  `valuesFileURL` must exist and be readable by this process at runtime.

  This method must be called before using `getNext()` with `mode = DIGEST_MODE`

  Parameters:
    `binCount` - the number of bins used in computing the empirical distribution
  Throws:
    `java.io.IOException` - if an error occurs reading the input file

### getMode

    public int getMode()

  Getter for property mode.

  Returns:
    Value of property mode.

### setMode

    public void setMode(int mode)

  Setter for property mode.

  Parameters:
    `mode` - New value of property mode.

### getValuesFileURL

    public java.net.URL getValuesFileURL()

  Getter for `valuesFileURL`

  ``
  Returns:
    Value of property valuesFileURL.

### setValuesFileURL

    public void setValuesFileURL(java.lang.String url)
                          throws java.net.MalformedURLException

  Sets the `valuesFileURL` using a string URL representation

  Parameters:
    `url` - String representation for new valuesFileURL.
  Throws:
    `java.net.MalformedURLException` - if url is not well formed

### setValuesFileURL

    public void setValuesFileURL(java.net.URL url)

  Sets the `valuesFileURL`

  Parameters:
    `url` - New value of property valuesFileURL.

### getEmpiricalDistribution

    public EmpiricalDistribution getEmpiricalDistribution()

  Getter for property empiricalDistribution.

  Returns:
    Value of property empiricalDistribution.

### resetReplayFile

    public void resetReplayFile()
                         throws java.io.IOException

  Resets REPLAY_MODE file pointer to the beginning of the `valuesFileURL`.

  Throws:
    `java.io.IOException` - if an error occurs opening the file

### closeReplayFile

    public void closeReplayFile()
                         throws java.io.IOException

  Closes `valuesFileURL` after use in REPLAY_MODE.

  Throws:
    `java.io.IOException` - if an error occurs closing the file

### getMu

    public double getMu()

  Getter for property mu.

  Returns:
    Value of property mu.

### setMu

    public void setMu(double mu)

  Setter for property mu.

  Parameters:
    `mu` - New value of property mu.

### getSigma

    public double getSigma()

  Getter for property sigma.

  Returns:
    Value of property sigma.

### setSigma

    public void setSigma(double sigma)

  Setter for property sigma.

  Parameters:
    `sigma` - New value of property sigma.

### getNextDigest

    private double getNextDigest()

  Gets a random value in DIGEST_MODE.

  Preconditions:

  - Before this method is called, `computeDistribution()` must have completed successfully; otherwise an `IllegalStateException` will be thrown

  Returns:
    next random value from the empirical distribution digest

### getNextReplay

    private double getNextReplay()
                          throws java.io.IOException

  Gets next sequential value from the `valuesFileURL`.

  Throws an IOException if the read fails.

  This method will open the `valuesFileURL` if there is no replay file open.

  The `valuesFileURL` will be closed and reopened to wrap around from EOF to BOF if EOF is encountered. EOFException (which is a kind of IOException) may still be thrown if the `valuesFileURL` is empty.

  Returns:
    next value from the replay file
  Throws:
    `java.io.IOException` - if there is a problem reading from the file
    `java.lang.NumberFormatException` - if an invalid numeric string is encountered in the file

### getNextUniform

    private double getNextUniform()

  Gets a uniformly distributed random value with mean = mu.

  Returns:
    random uniform value

### getNextExponential

    private double getNextExponential()

  Gets an exponentially distributed random value with mean = mu.

  Returns:
    random exponential value

### getNextGaussian

    private double getNextGaussian()

  Gets a Gaussian distributed random value with mean = mu and standard deviation = sigma.

  Returns:
    random Gaussian value

Copyright (c) 2003-2010 Apache Software Foundation