Advertisement
Guest User

Coriolis Dice Probabilities

a guest
Mar 17th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5.     private static final int DICE_COUNT = 10;
  6.     private static final int DICE_SIDES = 6;
  7.     private static final int ITERATIONS = 100_000_000;
  8.     private static final String TAB = "\t";
  9.  
  10.     private static final Random random = new Random();
  11.  
  12.     // First index is amount of dices rolled
  13.     // Second index is "at least amount of sixes" first without prayer then with prayer.
  14.     private static final int[][] results = new int[DICE_COUNT + 1][(DICE_COUNT + 1) * 2];
  15.  
  16.     private static final int[] workbench = new int[DICE_COUNT];
  17.  
  18.     public static void main(String[] args) {
  19.         for (int i = 0; i < ITERATIONS; i++) {
  20.             iterate();
  21.         }
  22.         printTable();
  23.     }
  24.  
  25.     private static void iterate() {
  26.         rollWorkbench();
  27.         takeNotes(false);
  28.         prayWorkbench();
  29.         takeNotes(true);
  30.     }
  31.  
  32.     private static int roll() {
  33.         return random.nextInt(DICE_SIDES) + 1;
  34.     }
  35.  
  36.     private static void rollWorkbench() {
  37.         for (int index = 0; index < DICE_COUNT; index++) {
  38.             int newValue = roll();
  39.             workbench[index] = newValue;
  40.         }
  41.     }
  42.  
  43.     private static void prayWorkbench() {
  44.         for (int index = 0; index < DICE_COUNT; index++) {
  45.             int oldValue = workbench[index];
  46.             if (oldValue != DICE_SIDES) {
  47.                 int newValue = roll();
  48.                 workbench[index] = newValue;
  49.             }
  50.         }
  51.     }
  52.  
  53.     private static void takeNotes(boolean prayer) {
  54.         int amountOfSixes = 0;
  55.         for (int index = 0; index < DICE_COUNT; index++) {
  56.             int value = workbench[index];
  57.             if (value == DICE_SIDES) {
  58.                 amountOfSixes += 1;
  59.             }
  60.  
  61.             int amountOfDices = index + 1;
  62.  
  63.             for (int atLeastAmountOfSixes = 0; atLeastAmountOfSixes <= amountOfSixes; atLeastAmountOfSixes++) {
  64.                 results[amountOfDices][atLeastAmountOfSixes * 2 + (prayer ? 1 : 0)] += 1;
  65.             }
  66.         }
  67.     }
  68.  
  69.     private static void printTable() {
  70.         final StringBuilder headingBuilder = new StringBuilder();
  71.  
  72.         headingBuilder.append("At least amount of sixes");
  73.         headingBuilder.append(TAB);
  74.  
  75.         headingBuilder.append("With Prayer");
  76.  
  77.         for (int amountOfDicesRolled = 0; amountOfDicesRolled <= DICE_COUNT; amountOfDicesRolled++) {
  78.             headingBuilder.append(TAB);
  79.             headingBuilder.append(String.format("%d dices rolled", amountOfDicesRolled));
  80.         }
  81.  
  82.         String heading = headingBuilder.toString();
  83.         System.out.println(heading);
  84.  
  85.         for (int resultIndex = 0; resultIndex < ((DICE_COUNT + 1) * 2); resultIndex++) {
  86.             final StringBuilder rowBuilder = new StringBuilder();
  87.  
  88.             final int atLeastAmountOfSixes = resultIndex / 2;
  89.             final boolean prayer = (resultIndex % 2 == 1);
  90.  
  91.             rowBuilder.append(atLeastAmountOfSixes);
  92.             rowBuilder.append(TAB);
  93.  
  94.             rowBuilder.append(prayer ? "yes" : "no");
  95.  
  96.             for (int amountOfDicesRolled = 0; amountOfDicesRolled <= DICE_COUNT; amountOfDicesRolled++) {
  97.                 final int result = results[amountOfDicesRolled][resultIndex];
  98.  
  99.                 rowBuilder.append(TAB);
  100.                 rowBuilder.append(asPercent(result));
  101.             }
  102.  
  103.             String row = rowBuilder.toString();
  104.             System.out.println(row);
  105.         }
  106.     }
  107.  
  108.     private static String asPercent(int result) {
  109.         return Math.round((double)result / (double)ITERATIONS * 100.0) + "%";
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement