Package org.apache.commons.math3.random
Class ValueServer
java.lang.Object
org.apache.commons.math3.random.ValueServer
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Always return mustatic final int
Use empirical distribution.static final int
Exponential random deviates with mean = μ.static final int
Gaussian random deviates with mean = μ, std dev = σ.static final int
Replay data from valuesFilePath.static final int
Uniform random deviates with mean = μ. -
Constructor Summary
ConstructorsConstructorDescriptionCreates new ValueServerValueServer
(RandomDataImpl randomData) Deprecated.ValueServer
(RandomGenerator generator) Construct a ValueServer instance using a RandomGenerator as its source of random data. -
Method Summary
Modifier and TypeMethodDescriptionvoid
ClosesvaluesFileURL
after use in REPLAY_MODE.void
Computes the empirical distribution using values from the file invaluesFileURL
, using the default number of bins.void
computeDistribution
(int binCount) Computes the empirical distribution using values from the file invaluesFileURL
andbinCount
bins.void
fill
(double[] values) Fills the input array with values generated using getNext() repeatedly.double[]
fill
(int length) Returns an array of lengthlength
with values generated using getNext() repeatedly.Returns theEmpiricalDistribution
used when operating in 0.int
getMode()
Returns the data generation mode.double
getMu()
double
getNext()
Returns the next generated value, generated according to the mode value (see MODE constants).double
getSigma()
Returns the standard deviation used when operating inGAUSSIAN_MODE
.Returns the URL for the file used to build the empirical distribution when usingDIGEST_MODE
.void
reSeed
(long seed) Reseeds the random data generator.void
Resets REPLAY_MODE file pointer to the beginning of thevaluesFileURL
.void
setMode
(int mode) Sets the data generation mode.void
setMu
(double mu) Sets themean
used in data generation.void
setSigma
(double sigma) Sets thestandard deviation
used inGAUSSIAN_MODE
.void
setValuesFileURL
(String url) Sets thevalues file URL
using a string URL representation.void
setValuesFileURL
(URL url) Sets the thevalues file URL
.
-
Field Details
-
DIGEST_MODE
public static final int DIGEST_MODEUse empirical distribution.- See Also:
-
REPLAY_MODE
public static final int REPLAY_MODEReplay data from valuesFilePath.- See Also:
-
UNIFORM_MODE
public static final int UNIFORM_MODEUniform random deviates with mean = μ.- See Also:
-
EXPONENTIAL_MODE
public static final int EXPONENTIAL_MODEExponential random deviates with mean = μ.- See Also:
-
GAUSSIAN_MODE
public static final int GAUSSIAN_MODEGaussian random deviates with mean = μ, std dev = σ.- See Also:
-
CONSTANT_MODE
public static final int CONSTANT_MODEAlways return mu- See Also:
-
-
Constructor Details
-
ValueServer
public ValueServer()Creates new ValueServer -
ValueServer
Deprecated.Construct a ValueServer instance using a RandomDataImpl as its source of random data.- Parameters:
randomData
- the RandomDataImpl instance used to source random data- Since:
- 3.0
-
ValueServer
Construct a ValueServer instance using a RandomGenerator as its source of random data.- Parameters:
generator
- source of random data- Since:
- 3.1
-
-
Method Details
-
getNext
Returns the next generated value, generated according to the mode value (see MODE constants).- Returns:
- generated value
- Throws:
IOException
- in REPLAY_MODE if a file I/O error occursMathIllegalStateException
- if mode is not recognizedMathIllegalArgumentException
- if the underlying random generator thwrows one
-
fill
public void fill(double[] values) throws IOException, MathIllegalStateException, MathIllegalArgumentException Fills the input array with values generated using getNext() repeatedly.- Parameters:
values
- array to be filled- Throws:
IOException
- in REPLAY_MODE if a file I/O error occursMathIllegalStateException
- if mode is not recognizedMathIllegalArgumentException
- if the underlying random generator thwrows one
-
fill
public double[] fill(int length) throws IOException, MathIllegalStateException, MathIllegalArgumentException Returns an array of lengthlength
with values generated using getNext() repeatedly.- Parameters:
length
- length of output array- Returns:
- array of generated values
- Throws:
IOException
- in REPLAY_MODE if a file I/O error occursMathIllegalStateException
- if mode is not recognizedMathIllegalArgumentException
- if the underlying random generator thwrows one
-
computeDistribution
Computes the empirical distribution using values from the file invaluesFileURL
, using the default number of bins.valuesFileURL
must exist and be readable by *this at runtime.This method must be called before using
getNext()
withmode = DIGEST_MODE
- Throws:
IOException
- if an I/O error occurs reading the input fileNullArgumentException
- if thevaluesFileURL
has not been setZeroException
- if URL contains no data
-
computeDistribution
public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException Computes the empirical distribution using values from the file invaluesFileURL
andbinCount
bins.valuesFileURL
must exist and be readable by this process at runtime.This method must be called before using
getNext()
withmode = DIGEST_MODE
- Parameters:
binCount
- the number of bins used in computing the empirical distribution- Throws:
NullArgumentException
- if thevaluesFileURL
has not been setIOException
- if an error occurs reading the input fileZeroException
- if URL contains no data
-
getMode
public int getMode()Returns the data generation mode. Seethe class javadoc
for description of the valid values of this property.- Returns:
- Value of property mode.
-
setMode
public void setMode(int mode) Sets the data generation mode.- Parameters:
mode
- New value of the data generation mode.
-
getValuesFileURL
Returns the URL for the file used to build the empirical distribution when usingDIGEST_MODE
.- Returns:
- Values file URL.
-
setValuesFileURL
Sets thevalues file URL
using a string URL representation.- Parameters:
url
- String representation for new valuesFileURL.- Throws:
MalformedURLException
- if url is not well formed
-
setValuesFileURL
Sets the thevalues file URL
.The values file must be an ASCII text file containing one valid numeric entry per line.
- Parameters:
url
- URL of the values file.
-
getEmpiricalDistribution
Returns theEmpiricalDistribution
used when operating in 0.- Returns:
- EmpircalDistribution built by
computeDistribution()
-
resetReplayFile
Resets REPLAY_MODE file pointer to the beginning of thevaluesFileURL
.- Throws:
IOException
- if an error occurs opening the fileNullPointerException
- if thevaluesFileURL
has not been set.
-
closeReplayFile
ClosesvaluesFileURL
after use in REPLAY_MODE.- Throws:
IOException
- if an error occurs closing the file
-
getMu
public double getMu()Returns the mean used when operating inGAUSSIAN_MODE
,EXPONENTIAL_MODE
orUNIFORM_MODE
. When operating inCONSTANT_MODE
, this is the constant value always returned. CallingcomputeDistribution()
sets this value to the overall mean of the values in thevalues file
.- Returns:
- Mean used in data generation.
-
setMu
public void setMu(double mu) Sets themean
used in data generation. Note that calling this method aftercomputeDistribution()
has been called will have no effect on data generated inDIGEST_MODE
.- Parameters:
mu
- new Mean value.
-
getSigma
public double getSigma()Returns the standard deviation used when operating inGAUSSIAN_MODE
. CallingcomputeDistribution()
sets this value to the overall standard deviation of the values in thevalues file
. This property has no effect when the data generation mode is notGAUSSIAN_MODE
.- Returns:
- Standard deviation used when operating in
GAUSSIAN_MODE
.
-
setSigma
public void setSigma(double sigma) Sets thestandard deviation
used inGAUSSIAN_MODE
.- Parameters:
sigma
- New standard deviation.
-
reSeed
public void reSeed(long seed) Reseeds the random data generator.- Parameters:
seed
- Value with which to reseed theRandomDataImpl
used to generate random data.
-
ValueServer(RandomGenerator)