Skip to content

Instantly share code, notes, and snippets.

@ganesshkumar
Created October 16, 2018 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganesshkumar/239e722fbd5ddc9744f732e9d3404cf0 to your computer and use it in GitHub Desktop.
Save ganesshkumar/239e722fbd5ddc9744f732e9d3404cf0 to your computer and use it in GitHub Desktop.
Royal Game of Ur - Dice roll distribution
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
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