Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Main {
- private static final int DICE_COUNT = 10;
- private static final int DICE_SIDES = 6;
- private static final int ITERATIONS = 100_000_000;
- private static final String TAB = "\t";
- private static final Random random = new Random();
- // First index is amount of dices rolled
- // Second index is "at least amount of sixes" first without prayer then with prayer.
- private static final int[][] results = new int[DICE_COUNT + 1][(DICE_COUNT + 1) * 2];
- private static final int[] workbench = new int[DICE_COUNT];
- public static void main(String[] args) {
- for (int i = 0; i < ITERATIONS; i++) {
- iterate();
- }
- printTable();
- }
- private static void iterate() {
- rollWorkbench();
- takeNotes(false);
- prayWorkbench();
- takeNotes(true);
- }
- private static int roll() {
- return random.nextInt(DICE_SIDES) + 1;
- }
- private static void rollWorkbench() {
- for (int index = 0; index < DICE_COUNT; index++) {
- int newValue = roll();
- workbench[index] = newValue;
- }
- }
- private static void prayWorkbench() {
- for (int index = 0; index < DICE_COUNT; index++) {
- int oldValue = workbench[index];
- if (oldValue != DICE_SIDES) {
- int newValue = roll();
- workbench[index] = newValue;
- }
- }
- }
- private static void takeNotes(boolean prayer) {
- int amountOfSixes = 0;
- for (int index = 0; index < DICE_COUNT; index++) {
- int value = workbench[index];
- if (value == DICE_SIDES) {
- amountOfSixes += 1;
- }
- int amountOfDices = index + 1;
- for (int atLeastAmountOfSixes = 0; atLeastAmountOfSixes <= amountOfSixes; atLeastAmountOfSixes++) {
- results[amountOfDices][atLeastAmountOfSixes * 2 + (prayer ? 1 : 0)] += 1;
- }
- }
- }
- private static void printTable() {
- final StringBuilder headingBuilder = new StringBuilder();
- headingBuilder.append("At least amount of sixes");
- headingBuilder.append(TAB);
- headingBuilder.append("With Prayer");
- for (int amountOfDicesRolled = 0; amountOfDicesRolled <= DICE_COUNT; amountOfDicesRolled++) {
- headingBuilder.append(TAB);
- headingBuilder.append(String.format("%d dices rolled", amountOfDicesRolled));
- }
- String heading = headingBuilder.toString();
- System.out.println(heading);
- for (int resultIndex = 0; resultIndex < ((DICE_COUNT + 1) * 2); resultIndex++) {
- final StringBuilder rowBuilder = new StringBuilder();
- final int atLeastAmountOfSixes = resultIndex / 2;
- final boolean prayer = (resultIndex % 2 == 1);
- rowBuilder.append(atLeastAmountOfSixes);
- rowBuilder.append(TAB);
- rowBuilder.append(prayer ? "yes" : "no");
- for (int amountOfDicesRolled = 0; amountOfDicesRolled <= DICE_COUNT; amountOfDicesRolled++) {
- final int result = results[amountOfDicesRolled][resultIndex];
- rowBuilder.append(TAB);
- rowBuilder.append(asPercent(result));
- }
- String row = rowBuilder.toString();
- System.out.println(row);
- }
- }
- private static String asPercent(int result) {
- return Math.round((double)result / (double)ITERATIONS * 100.0) + "%";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement