Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.