Class ContinuousUniformSampler
java.lang.Object
org.apache.commons.rng.sampling.distribution.SamplerBase
org.apache.commons.rng.sampling.distribution.ContinuousUniformSampler
- All Implemented Interfaces:
ContinuousSampler
,SharedStateContinuousSampler
,SharedStateSampler<SharedStateContinuousSampler>
- Direct Known Subclasses:
ContinuousUniformSampler.OpenIntervalContinuousUniformSampler
Sampling from a uniform distribution.
Sampling uses UniformRandomProvider.nextDouble()
.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
Specialization to sample from an open interval(lo, hi)
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double
Higher bound.private final double
Lower bound.private static final int
The minimum ULP gap for the open interval when the doubles have the opposite sign.private static final int
The minimum ULP gap for the open interval when the doubles have the same sign.private final UniformRandomProvider
Underlying source of randomness. -
Constructor Summary
ConstructorsConstructorDescriptionContinuousUniformSampler
(UniformRandomProvider rng, double lo, double hi) Create an instance. -
Method Summary
Modifier and TypeMethodDescription(package private) double
getHi()
Gets the higher bound.(package private) double
getLo()
Gets the lower bound.private static boolean
lessThanUnsigned
(long x, long y) Compares twolong
values numerically treating the values as unsigned to test if the first value is less than the second value.static SharedStateContinuousSampler
of
(UniformRandomProvider rng, double lo, double hi) Creates a new continuous uniform distribution sampler.static SharedStateContinuousSampler
of
(UniformRandomProvider rng, double lo, double hi, boolean excludeBounds) Creates a new continuous uniform distribution sampler.double
sample()
Creates adouble
sample.toString()
private static boolean
validateOpenInterval
(double lo, double hi) Check that the open interval is valid.Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.Methods inherited from class org.apache.commons.rng.sampling.distribution.SamplerBase
nextDouble, nextInt, nextInt, nextLong
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.commons.rng.sampling.distribution.ContinuousSampler
samples, samples
-
Field Details
-
MIN_ULP_SAME_SIGN
private static final int MIN_ULP_SAME_SIGNThe minimum ULP gap for the open interval when the doubles have the same sign.- See Also:
-
MIN_ULP_OPPOSITE_SIGN
private static final int MIN_ULP_OPPOSITE_SIGNThe minimum ULP gap for the open interval when the doubles have the opposite sign.- See Also:
-
lo
private final double loLower bound. -
hi
private final double hiHigher bound. -
rng
Underlying source of randomness.
-
-
Constructor Details
-
ContinuousUniformSampler
Create an instance.- Parameters:
rng
- Generator of uniformly distributed random numbers.lo
- Lower bound.hi
- Higher bound.
-
-
Method Details
-
sample
public double sample()Creates adouble
sample.- Specified by:
sample
in interfaceContinuousSampler
- Returns:
- a sample.
-
getLo
double getLo()Gets the lower bound. This is deliberately scoped as package private.- Returns:
- the lower bound
-
getHi
double getHi()Gets the higher bound. This is deliberately scoped as package private.- Returns:
- the higher bound
-
toString
- Overrides:
toString
in classSamplerBase
-
withUniformRandomProvider
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<SharedStateContinuousSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
of
Creates a new continuous uniform distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.lo
- Lower bound.hi
- Higher bound.- Returns:
- the sampler
- Since:
- 1.3
-
of
public static SharedStateContinuousSampler of(UniformRandomProvider rng, double lo, double hi, boolean excludeBounds) Creates a new continuous uniform distribution sampler.The bounds can be optionally excluded to sample from the open interval
(lower, upper)
. In this case if the bounds have the same sign the open interval must contain at least 1 double value between the limits; if the bounds have opposite signs the open interval must contain at least 2 double values between the limits excluding-0.0
. Thus the interval(-x,x)
will raise an exception whenx
isDouble.MIN_VALUE
.- Parameters:
rng
- Generator of uniformly distributed random numbers.lo
- Lower bound.hi
- Higher bound.excludeBounds
- Set totrue
to use the open interval(lower, upper)
.- Returns:
- the sampler
- Throws:
IllegalArgumentException
- If the open interval is invalid.- Since:
- 1.4
-
validateOpenInterval
private static boolean validateOpenInterval(double lo, double hi) Check that the open interval is valid. It must contain at least one double value between the limits if the signs are the same, or two double values between the limits if the signs are different (excluding-0.0
).- Parameters:
lo
- Lower bound.hi
- Higher bound.- Returns:
- false is the interval is invalid
-
lessThanUnsigned
private static boolean lessThanUnsigned(long x, long y) Compares twolong
values numerically treating the values as unsigned to test if the first value is less than the second value.See Long.compareUnsigned(long, long) in JDK 1.8.
- Parameters:
x
- the first valuey
- the second value- Returns:
- true if
x < y
-