Class UniformLongSampler
- java.lang.Object
-
- org.apache.commons.rng.sampling.distribution.UniformLongSampler
-
- All Implemented Interfaces:
LongSampler
,SharedStateLongSampler
,SharedStateSampler<SharedStateLongSampler>
- Direct Known Subclasses:
UniformLongSampler.FixedUniformLongSampler
,UniformLongSampler.LargeRangeUniformLongSampler
,UniformLongSampler.OffsetUniformLongSampler
,UniformLongSampler.PowerOf2RangeUniformLongSampler
,UniformLongSampler.SmallRangeUniformLongSampler
public abstract class UniformLongSampler extends java.lang.Object implements SharedStateLongSampler
Discrete uniform distribution sampler generating values of typelong
.Sampling uses
UniformRandomProvider.nextLong()
.When the range is a power of two the number of calls is 1 per sample. Otherwise a rejection algorithm is used to ensure uniformity. In the worst case scenario where the range spans half the range of a
long
(263 + 1) the expected number of calls is 2 per sample.- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
UniformLongSampler.FixedUniformLongSampler
Discrete uniform distribution sampler when the sample value is fixed.private static class
UniformLongSampler.LargeRangeUniformLongSampler
Discrete uniform distribution sampler when the range between lower and upper is too large to fit in a positive long.private static class
UniformLongSampler.OffsetUniformLongSampler
Adds an offset to an underlying discrete sampler.private static class
UniformLongSampler.PowerOf2RangeUniformLongSampler
Discrete uniform distribution sampler when the range is a power of 2 and greater than 1.private static class
UniformLongSampler.SmallRangeUniformLongSampler
Discrete uniform distribution sampler when the range is small enough to fit in a positive long.
-
Field Summary
Fields Modifier and Type Field Description protected UniformRandomProvider
rng
Underlying source of randomness.
-
Constructor Summary
Constructors Constructor Description UniformLongSampler(UniformRandomProvider rng)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static UniformLongSampler
createZeroBoundedSampler(UniformRandomProvider rng, long upper)
Create a new sampler for the range0
inclusive toupper
inclusive.private static boolean
isPowerOf2(long value)
Checks if the value is a power of 2.static UniformLongSampler
of(UniformRandomProvider rng, long lower, long upper)
Creates a new discrete uniform distribution sampler.java.lang.String
toString()
abstract UniformLongSampler
withUniformRandomProvider(UniformRandomProvider rng)
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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.distribution.LongSampler
sample, samples, samples
-
-
-
-
Field Detail
-
rng
protected final UniformRandomProvider rng
Underlying source of randomness.
-
-
Constructor Detail
-
UniformLongSampler
UniformLongSampler(UniformRandomProvider rng)
- Parameters:
rng
- Generator of uniformly distributed random numbers.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
withUniformRandomProvider
public abstract UniformLongSampler withUniformRandomProvider(UniformRandomProvider rng)
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<SharedStateLongSampler>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
-
of
public static UniformLongSampler of(UniformRandomProvider rng, long lower, long upper)
Creates a new discrete uniform distribution sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.lower
- Lower bound (inclusive) of the distribution.upper
- Upper bound (inclusive) of the distribution.- Returns:
- the sampler
- Throws:
java.lang.IllegalArgumentException
- iflower > upper
.
-
createZeroBoundedSampler
private static UniformLongSampler createZeroBoundedSampler(UniformRandomProvider rng, long upper)
Create a new sampler for the range0
inclusive toupper
inclusive.This can handle any positive
upper
.- Parameters:
rng
- Generator of uniformly distributed random numbers.upper
- Upper bound (inclusive) of the distribution. Must be positive.- Returns:
- the sampler
-
isPowerOf2
private static boolean isPowerOf2(long value)
Checks if the value is a power of 2.This returns
true
for the valueLong.MIN_VALUE
which can be handled as an unsigned long of 2^63.- Parameters:
value
- Value.- Returns:
true
if a power of 2
-
-