Distribution of outcomes: { '0': 81, '1': 108, '2': 54, '3': 12, '4': 1 }
Total number of outcomes: 256
Probability for each outcome: { '0': 0.31640625,
'1': 0.421875,
'2': 0.2109375,
'3': 0.046875,
'4': 0.00390625 }
Total probability: 1
Created
October 16, 2018 08:31
-
-
Save ganesshkumar/239e722fbd5ddc9744f732e9d3404cf0 to your computer and use it in GitHub Desktop.
Royal Game of Ur - Dice roll distribution
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
const diceSides = [0, 0, 0, 1]; | |
const dist = {}; | |
for (let i in diceSides) { | |
for (let j in diceSides) { | |
for (let k in diceSides) { | |
for (let l in diceSides) { | |
let sum = diceSides[i] + diceSides[j] + diceSides[k] + diceSides[l]; | |
if (!(sum in dist)) { | |
dist[sum] = 0; | |
} | |
dist[sum] += 1; | |
} | |
} | |
} | |
} | |
console.log("Distribution of outcomes: ", dist); | |
let total = 0; | |
const prob = {}; | |
Object.keys(dist).forEach(key => { | |
total += dist[key]; | |
}); | |
console.log("Total number of outcomes: ", total); | |
Object.keys(dist).forEach(key => { | |
prob[key] = dist[key] / total; | |
}); | |
console.log("Probability for each outcome: ", prob); | |
let totalProb = 0; | |
Object.keys(prob).forEach(key => (totalProb += prob[key])); | |
console.log("Total probability: ", totalProb); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment