From a36de7083753247290fb4a8d79d232542a58e01c Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Sat, 18 Sep 2021 14:38:37 +0200 Subject: [PATCH] implement Java 17 j.u.r.RandomGenerator#nextExponential() --- src/main/java/java8/util/SplittableRandom.java | 17 ++++++++++++++++- .../util/concurrent/ThreadLocalRandom.java | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/java8/util/SplittableRandom.java b/src/main/java/java8/util/SplittableRandom.java index 42623b7c..5f966a70 100644 --- a/src/main/java/java8/util/SplittableRandom.java +++ b/src/main/java/java8/util/SplittableRandom.java @@ -845,6 +845,22 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) false); } + /** + * Returns a nonnegative {@code double} value pseudorandomly chosen from + * an exponential distribution whose mean is 1. + * + * @return a nonnegative {@code double} value pseudorandomly chosen from an + * exponential distribution + * @since 17 + */ + public double nextExponential() { + double u; + do { + u = nextDouble(); + } while (u == 0.0 || u == 1.0); + return -Math.log(u); + } + /** * Return true if the implementation of RandomGenerator (algorithm) has been * marked for deprecation. @@ -858,7 +874,6 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) * * @return true if the implementation of RandomGenerator (algorithm) has been * marked for deprecation - * * @since 17 */ public boolean isDeprecated() { diff --git a/src/main/java/java8/util/concurrent/ThreadLocalRandom.java b/src/main/java/java8/util/concurrent/ThreadLocalRandom.java index 1cc21f77..ef0da382 100644 --- a/src/main/java/java8/util/concurrent/ThreadLocalRandom.java +++ b/src/main/java/java8/util/concurrent/ThreadLocalRandom.java @@ -732,6 +732,22 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) false); } + /** + * Returns a nonnegative {@code double} value pseudorandomly chosen from + * an exponential distribution whose mean is 1. + * + * @return a nonnegative {@code double} value pseudorandomly chosen from an + * exponential distribution + * @since 17 + */ + public double nextExponential() { + double u; + do { + u = nextDouble(); + } while (u == 0.0 || u == 1.0); + return -Math.log(u); + } + /** * Return true if the implementation of RandomGenerator (algorithm) has been * marked for deprecation. @@ -745,7 +761,6 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) * * @return true if the implementation of RandomGenerator (algorithm) has been * marked for deprecation - * * @since 17 */ public boolean isDeprecated() {