Skip to content

Dicey lab complete #51

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 1 commit 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
11 changes: 11 additions & 0 deletions binResults.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
2 : 27769: 0.03 **
3 : 55480: 0.06 *****
4 : 83443: 0.08 ********
5 : 111434: 0.11 ***********
6 : 138721: 0.14 *************
7 : 166952: 0.17 ****************
8 : 139571: 0.14 *************
9 : 110167: 0.11 ***********
10 : 82916: 0.08 ********
11 : 55762: 0.06 *****
12 : 27785: 0.03 **
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
24 changes: 24 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
import java.util.Arrays;

public class Bins {

int min;
int max;
Integer[] bins;

public Bins(int min, int max) {
this.min = min;
this.max = max;
bins = new Integer[max - min + 1];
Arrays.fill(bins, 0);
}

public int getBin(int binNumber) {
return bins[binNumber - min];
}

public int incrementBin(int binNumber) {
return bins[binNumber - min]++;
}

public Integer[] getBins() {
return bins;
}
}

21 changes: 21 additions & 0 deletions src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
import java.util.ArrayList;
import java.util.Random;

public class Dice {

int numOfDice;

public Dice(int numOfDice) {
this.numOfDice = numOfDice;
}

public Dice() {}

public int getNumberOfDice() {
return numOfDice;
}

public int tossAndSum() {
int sum = 0;
for(int i = 0; i < numOfDice; i++) {
sum += (Math.random() * 6) + 1;
}
return sum;
}
}
49 changes: 49 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
import java.util.ArrayList;
import java.util.List;

public class Simulation {

int numOfDice;
int numOfRolls;
Dice dice;
Bins bins;

public Simulation(int numOfDice, int numOfRolls) {
this.numOfDice = numOfDice;
this.numOfRolls = numOfRolls;
}

public void runSimulation(){
int result;
this.bins = new Bins(numOfDice, numOfDice * 6);
this.dice = new Dice(numOfDice);
for(int i = 0; i < numOfRolls; i++) {
result = dice.tossAndSum();
bins.incrementBin(result);
}
System.out.println("");
}

public void printResults() {
for(int i = numOfDice; i <= numOfDice * 6; i++) {
Double percentage = (double) bins.getBin(i) / numOfRolls;
System.out.printf(
"%2d : %7d: %.2f ", i, bins.getBin(i), percentage);
for(int j = 1; j < percentage * 100; j++) {
System.out.print("*");
}
System.out.println();
}

}

public double getPercentage(int numOfRolls, Bins bins) {
double percentage = 0;
for(int element : bins.getBins()) {
percentage = ((double) element / numOfRolls);
}
return percentage;
}

public static void main(String[] args) {
Simulation simulation = new Simulation(2, 1000000);
simulation.runSimulation();
simulation.printResults();
}

}
4 changes: 4 additions & 0 deletions src/test/java/BinsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class BinsTest {


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

public class DiceTest {

@Test
public void testGetNumberOfDice() {
//given
int expected = 2;
//when
Dice dice = new Dice(expected);
int actual = dice.getNumberOfDice();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void testTossAndSum() {
//given
int numOfDice = 2;
int expected = 8;
Dice dice = new Dice(2);
//when
int actual = dice.tossAndSum();
//then
System.out.println(actual);
}
}


// need number of dice
// need to roll dice
// need sum of dice