Skip to content

PaulResults.md #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions PaulResults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
***
Simulation of 2 dice tossed for 1000000 times.
***

2: 27588: 0.03 **
3: 55346: 0.06 *****
4: 83377: 0.08 ********
5: 110439: 0.11 ***********
6: 139244: 0.14 *************
7: 167051: 0.17 ****************
8: 139601: 0.14 *************
9: 111389: 0.11 ***********
10: 82921: 0.08 ********
11: 55351: 0.06 *****
12: 27693: 0.03 **

12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>


</project>
16 changes: 16 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Bins {
Map<Integer,Integer> binmap;
public Bins(int min , int max){
this.binmap = Stream
.iterate(min,q -> q + 1)
.limit(max)
.collect(Collectors.toMap( q -> q, q-> 0));
}
public Integer getBin(int bin){
return this.binmap.get(bin);
}
public void increment(int bin){
this.binmap.replace(bin,this.binmap.get(bin)+1);
}

}
29 changes: 29 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Dice {
private static class Die{
private int result;
private Die(){
this.result =(int) (Math.random()*(6 - 1 + 1) + 1);
}
private int roll(){
return (this.result = (int) (Math.random()*(6 - 1 + 1) + 1));
}
}
private final List<Die> dice;
public Dice(int numberOfDice){
dice = Stream
.generate(Die::new)
.limit(numberOfDice)
.collect(Collectors.toList());
}
public int tossAndSum(){
return dice.stream()
.mapToInt(Die::roll)
.sum();
}
public List<Die> getDice(){
return dice;
}


}
55 changes: 54 additions & 1 deletion src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
public class Simulation {
import com.sun.nio.sctp.SctpMultiChannel;

import java.util.stream.Stream;

public class Simulation {
private final Bins bins;
private final Dice dice;
private final int rolls;
private final int numDice;
public Simulation(int dice, int rolls) {
this.numDice = dice;
this.dice = new Dice(dice);
this.bins = new Bins(dice,dice * 6);
this.rolls = rolls;
}
public void runSimulation(){
Stream
.iterate(0,q-> q+1)
.limit(rolls)
.forEach(q-> {
int bin = dice.tossAndSum();
bins.increment(bin);
});
}

public String printResults() {
StringBuilder sb = new StringBuilder();
sb
.append("***\n" + "Simulation of ")
.append(this.numDice)
.append(" dice tossed for ")
.append(this.rolls)
.append(" times.\n")
.append("***\n")
.append("\n");
Stream
.iterate(this.numDice,q -> q + 1)
.limit((this.numDice) * 6L - 1)
.forEach(q -> {
sb
.append(String.format("%3s",q))
.append(String.format(":%9s",this.bins.getBin(q)))
.append(String.format(":%5.2f ", (double)(this.bins.getBin(q))/(double)(this.rolls)));
Stream
.iterate(0,r->r+1)
.limit(this.bins.getBin(q)/10_000)
.forEach(r -> sb.append("*"));
sb.append("\n");
});
return sb.toString();
}

public static void main(String[] args) {
Simulation sim = new Simulation(2,1_000_000);
sim.runSimulation();
System.out.println(sim.printResults());
}
}
28 changes: 28 additions & 0 deletions src/test/java/BinsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Optional;
import java.util.stream.Stream;

public class BinsTest {
private Bins bins;
@Before
public void setUp(){
bins = new Bins(2,12);
}
@Test
public void constructorAndGetTest(){
Stream
.iterate(2,q -> q+1)
.limit(12)
.forEach(q-> Assert.assertEquals(Integer.valueOf(0), bins.getBin(q)));
}
@Test
public void incrementTest(){
bins.increment(2);
Assert.assertEquals(Integer.valueOf(1),bins.getBin(2));
}


}
19 changes: 19 additions & 0 deletions src/test/java/DiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import org.junit.Assert;
import org.junit.Test;

public class DiceTest {
@Test
public void constructorTest(){
//when
Dice dice = new Dice(2);
//then
Assert.assertEquals(2,dice.getDice().size());
}
@Test
public void tossAndSumTest(){
//given
Dice dice = new Dice(2);
Assert.assertTrue(dice.tossAndSum() >= 2 && dice.tossAndSum() <= 12);
}

}
16 changes: 16 additions & 0 deletions src/test/java/SimulationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class SimulationTest {
private Simulation sim;
@Before
public void setUp(){
sim = new Simulation(2,1_000_000);
}
@Test
public void simulatorTest(){
sim.runSimulation();
Assert.assertNotNull(sim.printResults());
}
}