diff --git a/java-numbers-4/pom.xml b/java-numbers-4/pom.xml
new file mode 100644
index 0000000000..e1722fb039
--- /dev/null
+++ b/java-numbers-4/pom.xml
@@ -0,0 +1,52 @@
+
+ 4.0.0
+ java-numbers-4
+ java-numbers-4
+ jar
+
+
+ com.baeldung
+ parent-java
+ 0.0.1-SNAPSHOT
+ ../parent-java
+
+
+
+
+ io.vavr
+ vavr
+ ${vavr.version}
+
+
+ org.apache.commons
+ commons-lang3
+ ${commons.version}
+ test
+
+
+ org.assertj
+ assertj-core
+ ${assertj.version}
+ test
+
+
+
+
+ java-numbers-4
+
+
+ src/main/resources
+ true
+
+
+
+
+
+ 0.10.2
+ 3.9
+ 3.6.1
+
+
+
diff --git a/java-numbers-4/src/main/java/com/baeldung/probability/MaleHeightGenerator.java b/java-numbers-4/src/main/java/com/baeldung/probability/MaleHeightGenerator.java
new file mode 100644
index 0000000000..ed3fb8a578
--- /dev/null
+++ b/java-numbers-4/src/main/java/com/baeldung/probability/MaleHeightGenerator.java
@@ -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);
+ }
+}
diff --git a/java-numbers-4/src/main/java/com/baeldung/probability/RandomInvoker.java b/java-numbers-4/src/main/java/com/baeldung/probability/RandomInvoker.java
new file mode 100644
index 0000000000..66f82022ea
--- /dev/null
+++ b/java-numbers-4/src/main/java/com/baeldung/probability/RandomInvoker.java
@@ -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 random = Lazy.of(SplittableRandom::new);
+
+ public T withProbability(Supplier positiveCase, Supplier negativeCase, int probability) {
+ SplittableRandom random = this.random.get();
+ if (random.nextInt(1, 101) <= probability) {
+ return positiveCase.get();
+ } else {
+ return negativeCase.get();
+ }
+ }
+}
diff --git a/java-numbers-4/src/test/java/com/baeldung/probability/RandomInvokerUnitTest.java b/java-numbers-4/src/test/java/com/baeldung/probability/RandomInvokerUnitTest.java
new file mode 100644
index 0000000000..b08c086e30
--- /dev/null
+++ b/java-numbers-4/src/test/java/com/baeldung/probability/RandomInvokerUnitTest.java
@@ -0,0 +1,24 @@
+package com.baeldung.probability;
+
+import org.assertj.core.data.Offset;
+import org.junit.Test;
+
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RandomInvokerUnitTest {
+ @Test
+ public void givenProbability_whenInvoked_invokeWithProbability() {
+ RandomInvoker randomInvoker = new RandomInvoker();
+
+ int numberOfSamples = 1_000_000;
+ int probability = 10;
+ int howManyTimesInvoked = Stream.generate(() -> randomInvoker.withProbability(() -> 1, () -> 0, probability))
+ .limit(numberOfSamples)
+ .mapToInt(e -> e).sum();
+ int monteCarloProbability = (howManyTimesInvoked * 100) / numberOfSamples;
+
+ assertThat(monteCarloProbability).isCloseTo(probability, Offset.offset(1));
+ }
+}
diff --git a/pom.xml b/pom.xml
index 5ed3791760..b110cb0fe0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -343,18 +343,18 @@
animal-sniffer-mvn-plugin
annotations
antlr
-
- apache-cxf
- apache-libraries
- apache-olingo/olingo2
- apache-poi
+
+ apache-cxf
+ apache-libraries
+ apache-olingo/olingo2
+ apache-poi
apache-rocketmq
- apache-shiro
+ apache-shiro
apache-spark
apache-tapestry
apache-thrift
apache-tika
- apache-velocity
+ apache-velocity
asciidoctor
asm
@@ -443,7 +443,8 @@
java-lite
java-numbers
java-numbers-2
- java-numbers-3
+ java-numbers-3
+ java-numbers-4
java-rmi
java-spi
java-vavr-stream
@@ -856,18 +857,18 @@
animal-sniffer-mvn-plugin
annotations
antlr
-
- apache-cxf
- apache-libraries
- apache-olingo/olingo2
- apache-poi
+
+ apache-cxf
+ apache-libraries
+ apache-olingo/olingo2
+ apache-poi
apache-rocketmq
- apache-shiro
+ apache-shiro
apache-spark
apache-tapestry
apache-thrift
apache-tika
- apache-velocity
+ apache-velocity
asciidoctor
asm
@@ -956,6 +957,7 @@
java-numbers
java-numbers-2
java-numbers-3
+ java-numbers-4
java-rmi
java-spi
java-vavr-stream