Merge pull request #9758 from mdabrowski-eu/BAEL-4405-probability
BAEL-4405 Probability in Java
This commit is contained in:
commit
11ad6d048b
|
@ -0,0 +1,52 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-numbers-4</artifactId>
|
||||
<name>java-numbers-4</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>java-numbers-4</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<vavr.version>0.10.2</vavr.version>
|
||||
<commons.version>3.9</commons.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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> positiveCase, Supplier<T> negativeCase, int probability) {
|
||||
SplittableRandom random = this.random.get();
|
||||
if (random.nextInt(1, 101) <= probability) {
|
||||
return positiveCase.get();
|
||||
} else {
|
||||
return negativeCase.get();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
32
pom.xml
32
pom.xml
|
@ -343,18 +343,18 @@
|
|||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
<module>antlr</module>
|
||||
|
||||
<module>apache-cxf</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-poi</module>
|
||||
|
||||
<module>apache-cxf</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-rocketmq</module>
|
||||
<module>apache-shiro</module>
|
||||
<module>apache-shiro</module>
|
||||
<module>apache-spark</module>
|
||||
<module>apache-tapestry</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>apache-velocity</module>
|
||||
|
||||
<module>asciidoctor</module>
|
||||
<module>asm</module>
|
||||
|
@ -443,7 +443,8 @@
|
|||
<module>java-lite</module>
|
||||
<module>java-numbers</module>
|
||||
<module>java-numbers-2</module>
|
||||
<module>java-numbers-3</module>
|
||||
<module>java-numbers-3</module>
|
||||
<module>java-numbers-4</module>
|
||||
<module>java-rmi</module>
|
||||
<module>java-spi</module>
|
||||
<module>java-vavr-stream</module>
|
||||
|
@ -856,18 +857,18 @@
|
|||
<module>animal-sniffer-mvn-plugin</module>
|
||||
<module>annotations</module>
|
||||
<module>antlr</module>
|
||||
|
||||
<module>apache-cxf</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-poi</module>
|
||||
|
||||
<module>apache-cxf</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-olingo/olingo2</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-rocketmq</module>
|
||||
<module>apache-shiro</module>
|
||||
<module>apache-shiro</module>
|
||||
<module>apache-spark</module>
|
||||
<module>apache-tapestry</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>apache-velocity</module>
|
||||
|
||||
<module>asciidoctor</module>
|
||||
<module>asm</module>
|
||||
|
@ -956,6 +957,7 @@
|
|||
<module>java-numbers</module>
|
||||
<module>java-numbers-2</module>
|
||||
<module>java-numbers-3</module>
|
||||
<module>java-numbers-4</module>
|
||||
<module>java-rmi</module>
|
||||
<module>java-spi</module>
|
||||
<module>java-vavr-stream</module>
|
||||
|
|
Loading…
Reference in New Issue