Skip to content
This repository was archived by the owner on Feb 25, 2024. It is now read-only.

Commit c904d56

Browse files
committed
implement Java 17 j.u.r.RandomGenerator#nextExponential()
1 parent 9e7830c commit c904d56

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/main/java/java9/util/SplittableRandom.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,22 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
843843
false);
844844
}
845845

846+
/**
847+
* Returns a nonnegative {@code double} value pseudorandomly chosen from
848+
* an exponential distribution whose mean is 1.
849+
*
850+
* @return a nonnegative {@code double} value pseudorandomly chosen from an
851+
* exponential distribution
852+
* @since 17
853+
*/
854+
public double nextExponential() {
855+
double u;
856+
do {
857+
u = nextDouble();
858+
} while (u == 0.0 || u == 1.0);
859+
return -Math.log(u);
860+
}
861+
846862
/**
847863
* Return true if the implementation of RandomGenerator (algorithm) has been
848864
* marked for deprecation.
@@ -856,7 +872,6 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
856872
*
857873
* @return true if the implementation of RandomGenerator (algorithm) has been
858874
* marked for deprecation
859-
*
860875
* @since 17
861876
*/
862877
public boolean isDeprecated() {

src/main/java/java9/util/concurrent/ThreadLocalRandom.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,22 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
729729
false);
730730
}
731731

732+
/**
733+
* Returns a nonnegative {@code double} value pseudorandomly chosen from
734+
* an exponential distribution whose mean is 1.
735+
*
736+
* @return a nonnegative {@code double} value pseudorandomly chosen from an
737+
* exponential distribution
738+
* @since 17
739+
*/
740+
public double nextExponential() {
741+
double u;
742+
do {
743+
u = nextDouble();
744+
} while (u == 0.0 || u == 1.0);
745+
return -Math.log(u);
746+
}
747+
732748
/**
733749
* Return true if the implementation of RandomGenerator (algorithm) has been
734750
* marked for deprecation.
@@ -742,7 +758,6 @@ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound)
742758
*
743759
* @return true if the implementation of RandomGenerator (algorithm) has been
744760
* marked for deprecation
745-
*
746761
* @since 17
747762
*/
748763
public boolean isDeprecated() {

0 commit comments

Comments
 (0)