Created
January 18, 2023 18:31
-
-
Save tomwhoiscontrary/f5a35f3170ce953bd2cdc0565eac5070 to your computer and use it in GitHub Desktop.
brute force standard array roller for https://rpg.stackexchange.com/q/204051
This file contains hidden or 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
import java.util.Arrays; | |
import java.util.Random; | |
public class CalculateStandardArray { | |
private static final Random RANDOM = new Random(); | |
public static void main(String[] args) { | |
int numStats = 12; | |
int numTrials = 10_000_000; | |
int[] totals = new int[numStats]; | |
int[] rolls = new int[numStats]; | |
double[] means = new double[numStats]; | |
while (true) { | |
long startNanos = System.nanoTime(); | |
Arrays.fill(totals, 0); | |
for (int j = 0; j < numTrials; j++) { | |
for (int i = 0; i < numStats; i++) rolls[i] = roll3DT(); // change to roll3D6 or roll4DTDropLowest, etc | |
Arrays.sort(rolls); | |
for (int i = 0; i < numStats; i++) totals[i] += rolls[i]; | |
} | |
for (int i = 0; i < numStats; i++) means[i] = (double) totals[i] / numTrials; | |
long endNanos = System.nanoTime(); | |
double elapsedSec = (endNanos - startNanos) / 1e9; | |
System.out.println(Arrays.toString(means) + " " + elapsedSec); | |
} | |
} | |
private static int roll3DT() { | |
return rollDT() + rollDT() + rollDT(); | |
} | |
private static int roll4DTDropLowest() { | |
return dropLowest(rollDT(), rollDT(), rollDT(), rollDT()); | |
} | |
private static int roll3D6() { | |
return rollD6() + rollD6() + rollD6(); | |
} | |
private static int roll4D6DropLowest() { | |
return dropLowest(rollD6(), rollD6(), rollD6(), rollD6()); | |
} | |
private static int dropLowest(int a, int b, int c, int d) { | |
int lowest = Math.min(Math.min(a, b), Math.min(c, d)); | |
return a + b + c + d - lowest; | |
} | |
private static final int[] DT_FACES = new int[]{2, 3, 3, 4, 5, 6}; | |
private static int rollDT() { | |
return DT_FACES[RANDOM.nextInt(6)]; | |
} | |
private static int rollD6() { | |
return RANDOM.nextInt(6) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment