Skip to content

Instantly share code, notes, and snippets.

@ZReC
Created March 29, 2022 07:06
Show Gist options
  • Save ZReC/f4263fc3b1b278612041df38fd1c1bb9 to your computer and use it in GitHub Desktop.
Save ZReC/f4263fc3b1b278612041df38fd1c1bb9 to your computer and use it in GitHub Desktop.
roll two six-sided dice a million times
// https://mathworld.wolfram.com/Dice.html
const DICE = 2;
const SIDES = 6;
const ROLLS = 1e6;
// init count
const count: { [sum: number]: number } = {};
for (let i = DICE; i <= DICE * SIDES; i++) {
count[i] = 0;
}
// roll dice
for (let i = 0; i < ROLLS; i++) {
let sum = 0;
for (let j = 0; j < DICE; j++) {
sum += 1 + ~~(Math.random() * SIDES);
}
count[sum]++;
}
// log sum distribution
Object
.keys(count)
.forEach(
v => console.log(`${v}: ${(count[v] / ROLLS) * 100}%`)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment