BAEL-4405 move probability examples to java-numbers-4
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.probability;
|
||||
|
||||
import org.apache.commons.math3.distribution.NormalDistribution;
|
||||
|
||||
public class MaleHeightGenerator {
|
||||
private static final double MEAN_HEIGHT = 176.02;
|
||||
private static final double STANDARD_DEVIATION = 7.11;
|
||||
private static NormalDistribution distribution = new NormalDistribution(MEAN_HEIGHT, STANDARD_DEVIATION);
|
||||
|
||||
public static double generateNormalHeight() {
|
||||
return distribution.sample();
|
||||
}
|
||||
|
||||
public static double probabilityOfHeightBetween(double heightLowerExclusive, double heightUpperInclusive) {
|
||||
return distribution.probability(heightLowerExclusive, heightUpperInclusive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.probability;
|
||||
|
||||
import io.vavr.Lazy;
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class RandomInvoker {
|
||||
private final Lazy<SplittableRandom> random = Lazy.of(SplittableRandom::new);
|
||||
|
||||
public <T> T withProbability(Supplier<T> supplier1, Supplier<T> supplier2, int probability) {
|
||||
SplittableRandom random = this.random.get();
|
||||
if (random.nextInt(1, 101) <= probability) {
|
||||
return supplier1.get();
|
||||
} else {
|
||||
return supplier2.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user