Created
April 25, 2019 06:54
-
-
Save Glorfindel83/d71640ca52c173b852ca7c05bd285db3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackexchange.rpg; | |
import java.util.Random; | |
public class Statistics { | |
public static void main(String[] args) { | |
long total = 0, samples = 0; | |
while (true) { | |
samples++; | |
total += sample(); | |
if (shouldPrint(samples)) { | |
System.out.println("N=" + samples + ", average: " + String.format("%.3f", total / (double)samples)); | |
} | |
} | |
} | |
private static final Random random = new Random(); | |
private static int get4d6DropLowest() { | |
int lowest = 6, sum = 0; | |
for (int i = 0; i < 4; i++) { | |
int die = 1 + random.nextInt(6); | |
sum += die; | |
lowest = Math.min(lowest, die); | |
} | |
return sum - lowest; | |
} | |
// https://www.d20pfsrd.com/basics-ability-scores/ability-scores/ | |
private static final int[] pointsPerScore = { -4, -4, -4, -4, -4, -4, -4, -4, -2, -1, 0, 1, 2, 3, 5, 7, 10, 13, | |
17 }; | |
private static long sample() { | |
// Initialize matrix | |
int[][] matrix = new int[6][6]; | |
for (int i = 0; i < 6; i++) { | |
for (int j = 0; j < 6; j++) { | |
matrix[i][j] = get4d6DropLowest(); | |
} | |
} | |
// Compute maximum # of points | |
int highest = 0, points = 0; | |
for (int i = 0; i < 6; i++) { | |
// i-th column | |
points = 0; | |
for (int j = 0; j < 6; j++) { | |
points += pointsPerScore[matrix[i][j]]; | |
} | |
highest = Math.max(highest, points); | |
// i-th row | |
points = 0; | |
for (int j = 0; j < 6; j++) { | |
points += pointsPerScore[matrix[j][i]]; | |
} | |
highest = Math.max(highest, points); | |
} | |
// Diagonal | |
points = 0; | |
for (int i = 0; i < 6; i++) { | |
points += pointsPerScore[matrix[i][i]]; | |
} | |
highest = Math.max(highest, points); | |
// Antidiagonal | |
points = 0; | |
for (int i = 0; i < 6; i++) { | |
points += pointsPerScore[matrix[i][5 - i]]; | |
} | |
highest = Math.max(highest, points); | |
return highest; | |
} | |
private static boolean shouldPrint(long n) { | |
while (true) { | |
if (n == 1 || n == 3) | |
return true; | |
if (n % 10 != 0) | |
return false; | |
n /= 10; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment