diff --git a/java/src/main/java/Bins.java b/java/src/main/java/Bins.java index b9da83e..84d75d6 100644 --- a/java/src/main/java/Bins.java +++ b/java/src/main/java/Bins.java @@ -1,4 +1,22 @@ public class Bins { + private int[] counts; // Array to store the count of each possible outcome + // Constructor to initialize the bins based on the number of dice and sides + public Bins(int size) { + this.counts = new int[size]; + } + // Method to increment the count for a specific outcome + public void increment(int value) { + counts[value]++; + } + + // Method to get the count for a specific outcome + public int getCount(int value) { + return counts[value]; + } + + public int getSize() { + return counts.length; + } } diff --git a/java/src/main/java/Dice.java b/java/src/main/java/Dice.java index 2283c96..8e3a878 100644 --- a/java/src/main/java/Dice.java +++ b/java/src/main/java/Dice.java @@ -1,4 +1,22 @@ +import java.util.Random; + public class Dice { + private int sides; + private int numberOfDice; // stores the number of dice to be rolled + private Random random; + + public Dice(int sides, int numberOfDice) { + this.sides = sides; + this.numberOfDice = numberOfDice; + this.random = new Random(); + } + public int roll() { + int total = 0; + for (int i = 0; i < numberOfDice; i++) { // loop to roll each die one by one + total += random.nextInt(sides) + 1; // adds each roll to the running total + } + return total; + } } diff --git a/java/src/main/java/Simulation.java b/java/src/main/java/Simulation.java index 73d86e8..bf65106 100644 --- a/java/src/main/java/Simulation.java +++ b/java/src/main/java/Simulation.java @@ -1,5 +1,39 @@ public class Simulation { +public static void main(String[] args) { + Dice dice = new Dice(6, 2); // Create a dice object with 6 sides and n dice to roll + Bins bins = new Bins(13); // Create bins to store the counts of each possible outcome + int numrolls = 1000000; // Number of rolls to simulate + System.out.println(); + System.out.println("Rolling the dice..."); + for (int i = 0; i < numrolls; i++) { // loop to roll the dice n times + int result = dice.roll(); + bins.increment(result); // Increment the count for the rolled result + System.out.println("Roll " + (i + 1) + ": " + " Total: " + result); //print result + } + System.out.println("\nResults:"); + for (int i = 2; i<= 12; i++) { + double percentage = (bins.getCount(i) * 100.0) / numrolls; + System.out.printf("Total %d: %5d times (%.2f%%)%n", i, bins.getCount(i), percentage); + } + + System.out.println("\nHistogram: "); + for (int i = 2; i <= 12; i++) { + double percentage = (bins.getCount(i) * 100.0) / numrolls; + String bar = makeBar(percentage); + System.out.printf("Total %2d: %-20s %.2f%%%n", i, bar, percentage); + } +} + +public static String makeBar(double percentage) { + int blocks = (int) percentage; + String bar = ""; + for (int i = 0; i < blocks; i++) { + bar += "*"; + } + return bar; } +} + diff --git a/java/target/classes/Bins.class b/java/target/classes/Bins.class new file mode 100644 index 0000000..21f4c5b 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..9727645 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..8881b53 Binary files /dev/null and b/java/target/classes/Simulation.class differ