Skip to content

Dicey #47

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 3 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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>Dicey-Lab</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
14 changes: 14 additions & 0 deletions src/main/MannyResults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~
Simulation of 2 dice tossed for 1,000,000 times
~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~
2 : 27575 : 0.03 **
3 : 55480 : 0.06 *****
4 : 83778 : 0.08 ********
5 : 110976 : 0.11 ***********
6 : 138851 : 0.14 *************
7 : 167160 : 0.17 ****************
8 : 138546 : 0.14 *************
9 : 111153 : 0.11 ***********
10 : 83432 : 0.08 ********
11 : 55314 : 0.06 *****
12 : 27735 : 0.03 **
28 changes: 28 additions & 0 deletions src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
import java.util.Arrays;

public class Bins {

private int min;
private int max;
Integer[] sumOfRolls ;

public Bins(int min, int max) {
this.min = min;
this.max = max;
this.sumOfRolls = new Integer[(max - min) + 1];

Arrays.fill(sumOfRolls, 0);
}
// for (int i = 0; i < sumOfRolls.length; i++) {
// sumOfRolls[i] = 0;
// }


public Integer getBin(int sum) { // getting the sum of rolls for the specific number you're tracking
int indexOfSum = sum - min;
return sumOfRolls[indexOfSum];
}

public void incrementBin(int binIndex){
int index = binIndex - min;
sumOfRolls[index]++;
}


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

private final int numberOfDice;

public Dice(int diceNum) {
this.numberOfDice = diceNum;
}

public int tossAndSum(){
int sum = 0;
for (int i = 0; i < numberOfDice; i++) {
sum += (int) ((Math.random() * 6) + 1);
}
return sum;
}

}


46 changes: 46 additions & 0 deletions src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import java.util.Arrays;

public class Simulation {

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

int numberOfDice;
int numberOfRolls;
Integer[] results;

public Simulation(int numberOfDice, int numberOfRolls) {
this.numberOfDice = numberOfDice;
this.numberOfRolls = numberOfRolls;
}

public Integer[] runSimulation() {

Dice testDice = new Dice(numberOfDice);
Bins testBin = new Bins(numberOfDice, numberOfDice * 6);

for (int i = 0; i < numberOfRolls; i++) {
int toss = testDice.tossAndSum();
testBin.incrementBin(toss);
}
return testBin.sumOfRolls;
}

private void printResults() {
Integer[] results = runSimulation();
System.out.println("~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~\n"
+ "Simulation of 2 dice tossed for 1,000,000 times\n"
+ "~*~*~*~*~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~~*~*~");
for(int i = 0; i < results.length; i++) {
int index = i + 2;
int frequency = results[i];
float percent = (float) results[i] / this.numberOfRolls;
int starValue = (int) (percent * 100);
System.out.printf("%2d : %8d : %.2f ", index, frequency, percent);
for (int j = 0; j < starValue; j++) {
System.out.print("*");
}
System.out.println();
}

}

}
19 changes: 19 additions & 0 deletions src/test/java/BinsTest.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 BinsTest {
@Test
public void testBins() {
// Given
int testMin = 2;
int testMax = 12;
int expected = testMax - testMin + 1;

// When
Bins testBin = new Bins(testMin, testMax);
int actual = testBin.sumOfRolls.length;

// Then
Assert.assertEquals(expected, actual);
}
}
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 testDice() {
// Given
int numberOfDice = 2;
Dice testDice = new Dice(numberOfDice);

// When
Integer test = testDice.tossAndSum();

// Then
Assert.assertTrue(test < 13 && test > 1);
}

}