Created
March 29, 2022 07:06
-
-
Save ZReC/f4263fc3b1b278612041df38fd1c1bb9 to your computer and use it in GitHub Desktop.
roll two six-sided dice a million times
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
// 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