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
18 changes: 18 additions & 0 deletions java/src/main/java/Bins.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions java/src/main/java/Dice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions java/src/main/java/Simulation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

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.