Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Last active December 13, 2020 10:03
Show Gist options
  • Save Sawtaytoes/a307f7544003c80efac7a48b86471466 to your computer and use it in GitHub Desktop.
Save Sawtaytoes/a307f7544003c80efac7a48b86471466 to your computer and use it in GitHub Desktop.
Probability calculation of dice rolls in Valeria: Card Kingdoms.
// Copy-paste this into a JavaScript console to execute.
probabilities = (
[
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9, 10],
[11, 12],
]
.map((
expectedNumbers,
) => (
expectedNumbers
.map((
expectedNumber,
) => (
Array(10000)
.fill()
.map(() => ({
die1: Math.ceil(Math.random() * 6),
die2: Math.ceil(Math.random() * 6),
}))
.map(({
die1,
die2,
}) => ({
additive: die1 + die2,
die1,
die2,
}))
.reduce(
(
count,
{
additive,
die1,
die2,
},
) => (
die1 === expectedNumber
|| die2 === expectedNumber
|| additive === expectedNumber
? count + 1
: count
),
0,
)
))
.map((
count
) => (
count / 10000
))
.reduce(
(
totalProbability,
probability,
) => (
totalProbability + probability
),
0,
)
))
.map((
probability,
) => (
(probability * 100)
.toFixed(2)
))
)
JSON
.stringify(
probabilities,
null,
2,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment