diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..614204b Binary files /dev/null and b/.DS_Store differ diff --git a/java/src/main/java/Bins.java b/java/src/main/java/Bins.java index b9da83e..5f43ef0 100644 --- a/java/src/main/java/Bins.java +++ b/java/src/main/java/Bins.java @@ -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; + } +} \ No newline at end of file diff --git a/java/src/main/java/Dice.java b/java/src/main/java/Dice.java index 2283c96..6777bb6 100644 --- a/java/src/main/java/Dice.java +++ b/java/src/main/java/Dice.java @@ -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; + } +} \ No newline at end of file diff --git a/java/src/main/java/Simulation.java b/java/src/main/java/Simulation.java index 73d86e8..14cd44b 100644 --- a/java/src/main/java/Simulation.java +++ b/java/src/main/java/Simulation.java @@ -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(); + } +} \ No newline at end of file diff --git a/java/target/classes/Bins.class b/java/target/classes/Bins.class new file mode 100644 index 0000000..54be45f Binary files /dev/null and b/java/target/classes/Bins.class differ diff --git a/java/target/classes/Dice.class b/java/target/classes/Dice.class new file mode 100644 index 0000000..4ba4f76 Binary files /dev/null and b/java/target/classes/Dice.class differ diff --git a/java/target/classes/Simulation.class b/java/target/classes/Simulation.class new file mode 100644 index 0000000..bb90905 Binary files /dev/null and b/java/target/classes/Simulation.class differ