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.
44 changes: 42 additions & 2 deletions java/src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@

public class Bins {
private int min;
private int max;
private int[] bins;

public Bins(int min, int max) {
if (min > max) {
throw new IllegalArgumentException("Invalid range");
}
this.min = min;
this.max = max;
bins = new int[max - min + 1];
}

public void incrementBin(int value) {
if (value < min || value > max) {
throw new IllegalArgumentException("Out of range");
}
bins[value - min]++;
}

public int getBin(int value) {
if (value < min || value > max) {
throw new IllegalArgumentException("Out of range");
}
return bins[value - min];
}

public int getTotal() {
int total = 0;
for (int count : bins) {
total += count;
}
return total;
}

public int getMin() {
return min;
}

}
public int getMax() {
return max;
}
}
24 changes: 23 additions & 1 deletion java/src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import java.util.Random;

public class Dice {
private int numberOfDice;
private Random random;

public Dice(int numberOfDice) {
if (numberOfDice <= 0) {
throw new IllegalArgumentException("Number of dice must be > 0");
}
this.numberOfDice = numberOfDice;
this.random = new Random();
}

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

}
public int getNumberOfDice() {
return numberOfDice;
}
}
45 changes: 44 additions & 1 deletion java/src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
public class Simulation {

private int numberOfDice;
private int numberOfTosses;
private Dice dice;
private Bins results;

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

}
int min = numberOfDice;
int max = numberOfDice * 6;

dice = new Dice(numberOfDice);
results = new Bins(min, max);
}

public void runSimulation() {
for (int i = 0; i < numberOfTosses; i++) {
int sum = dice.tossAndSum();
results.incrementBin(sum);
}
}

public void printResults() {

System.out.println("Simulation of " + numberOfDice + " dice tossed for " + numberOfTosses + " times.");

int total = results.getTotal();

for (int i = results.getMin(); i <= results.getMax(); i++) {
int count = results.getBin(i);
double percent = (double) count / total;

int stars = (int) (percent * 100);
String bar = "*".repeat(stars);

System.out.printf("%2d : %8d: %.2f %s%n", i, count, percent, bar);
}
}

public static void main(String[] args) {
Simulation sim = new Simulation(2, 1_000_000);
sim.runSimulation();
sim.printResults();
}
}
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.