Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file added java/.DS_Store
Binary file not shown.
Binary file added java/src/.DS_Store
Binary file not shown.
Empty file added java/src/MichaelSieResults.md
Empty file.
Binary file added java/src/main/.DS_Store
Binary file not shown.
Binary file added java/src/main/java/Bins.class
Binary file not shown.
21 changes: 21 additions & 0 deletions java/src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

public class Bins {
int s;
int[] x;
int minimum;
int maximum;

public Bins(int min, int max) {
minimum = min;
maximum = max;
s = max - min + 1;
x = new int[s];
}

public void incrementBin(int value){
int i = value - minimum;
x[i]++;
}

public int getBin(int value){
int i = value - minimum;
return x[i];
}

}
Binary file added java/src/main/java/Dice.class
Binary file not shown.
21 changes: 20 additions & 1 deletion java/src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

import java.util.Random;


public class Dice {
int d;

public Dice(int n){
d = n;
}

public int tossAndSum() {
Random rand = new Random();
int r;
int sum = 0;
for(int i= 0; i < d; i++){
r = rand.nextInt(6-1+1)+1;
sum = sum + r;
}
return sum;
}

}
}
Binary file added java/src/main/java/Simulation.class
Binary file not shown.
68 changes: 68 additions & 0 deletions java/src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
import java.util.Scanner;

public class Simulation {
int nDice;
int nToss;
int min;
int max;
Dice dice;
Bins bins;



public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int nDice = getInput(scanner, "Please enter number of dice: ");
int nToss = getInput(scanner, "Please enter number of toss: ");

Simulation sim = new Simulation(nDice, nToss);
sim.runSimulation();
sim.printResult();
scanner.close();

}

public Simulation(int nDice, int nToss){
this.nDice = nDice;
this.nToss = nToss;
min = nDice;
max = nDice * 6;
dice = new Dice(nDice);
bins = new Bins(min, max);
}

public void runSimulation(){
for(int i = 1; i <= nToss; i++){
int result = dice.tossAndSum();
bins.incrementBin(result);
}
}

public void printResult(){
for(int i = min; i <= max; i++){
int count = bins.getBin(i);
double prob = (double) count / nToss;
int starsCount = (int) (prob * 100);
System.out.printf("%2d : %7d : %.2f ", i, count, prob);
for(int j = 0; j < starsCount; j++){
System.out.print("*");
}
System.out.println();
}
}

public static int getInput(Scanner scanner, String Prompt){
while(true){
System.out.print(Prompt);
if(!scanner.hasNextInt()){
System.out.println("Invalid Input. Please input a number.");
scanner.next();
continue;
}
int value = scanner.nextInt();

if(value <= 0){
System.out.println("Please enter positive number.");
continue;
}
return value;
}

}
}

Binary file added java/target/classes/Bins.class
Binary file not shown.
Binary file added java/target/classes/Dice.class
Binary file not shown.
Binary file added java/target/classes/Simulation.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Users/michael/Projects/DiceyLab/java/src/main/java/Simulation.java
/Users/michael/Projects/DiceyLab/java/src/main/java/Bins.java
/Users/michael/Projects/DiceyLab/java/src/main/java/Dice.java