Skip to content

Commit 127e14d

Browse files
committed
RNG-190: Benchmark sampler generation of an open interval (0, 1)
1 parent bbbba93 commit 127e14d

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ContinuousSamplersPerformance.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ public void setup() {
144144
sampler = ChengBetaSampler.of(rng, 0.45, 6.7);
145145
} else if ("ContinuousUniformSampler".equals(samplerType)) {
146146
sampler = ContinuousUniformSampler.of(rng, 123.4, 5678.9);
147+
} else if ("ContinuousUniformSamplerDouble".equals(samplerType)) {
148+
sampler = ContinuousUniformSampler.of(rng, 0, 1);
149+
} else if ("ContinuousUniformSamplerOpenDouble".equals(samplerType)) {
150+
sampler = ContinuousUniformSampler.of(rng, 0, 1, true);
151+
} else if ("UniformOpenDoubleBits".equals(samplerType)) {
152+
sampler = new UniformOpenDouble(rng)::sampleUsingBitsToDouble;
153+
} else if ("UniformOpenDoubleMultiply".equals(samplerType)) {
154+
sampler = new UniformOpenDouble(rng)::sampleUsingMultiply53bits;
155+
} else if ("UniformOpenDoubleRejection".equals(samplerType)) {
156+
sampler = new UniformOpenDouble(rng)::sampleUsingRejection;
157+
} else if ("UniformOpenDoubleRecursion".equals(samplerType)) {
158+
sampler = new UniformOpenDouble(rng)::sampleUsingRecursion;
147159
} else if ("InverseTransformParetoSampler".equals(samplerType)) {
148160
sampler = InverseTransformParetoSampler.of(rng, 23.45, 0.1234);
149161
} else if ("StableSampler".equals(samplerType)) {
@@ -154,6 +166,58 @@ public void setup() {
154166
}
155167
}
156168

169+
/**
170+
* Specialization to sample from an open interval {@code (0, 1)}.
171+
* See RNG-190.
172+
*/
173+
private static final class UniformOpenDouble {
174+
/** RNG. */
175+
private final UniformRandomProvider source;
176+
177+
/**
178+
* @param rng Generator of uniformly distributed random numbers.
179+
*/
180+
UniformOpenDouble(UniformRandomProvider rng) {
181+
this.source = rng;
182+
}
183+
184+
/**
185+
* @return the double in (0, 1)
186+
*/
187+
public double sampleUsingBitsToDouble() {
188+
return Double.longBitsToDouble((source.nextLong() >>> 12) | 0x3ff0000000000001L) - 1.0;
189+
}
190+
191+
/**
192+
* @return the double in (0, 1)
193+
*/
194+
public double sampleUsingMultiply53bits() {
195+
return ((source.nextLong() >>> 11) | 1L) * 0x1.0p-53d;
196+
}
197+
198+
/**
199+
* @return the double in (0, 1)
200+
*/
201+
public double sampleUsingRejection() {
202+
long a;
203+
do {
204+
a = source.nextLong() >>> 11;
205+
} while (a == 0);
206+
return a * 0x1.0p-53d;
207+
}
208+
209+
/**
210+
* @return the double in (0, 1)
211+
*/
212+
public double sampleUsingRecursion() {
213+
final double a = (source.nextLong() >>> 11) * 0x1.0p-53d;
214+
if (a == 0.0) {
215+
return sampleUsingRecursion();
216+
}
217+
return a;
218+
}
219+
}
220+
157221
// Benchmarks methods below.
158222

159223
/**

0 commit comments

Comments
 (0)