Skip to content

Instantly share code, notes, and snippets.

@shiv122
Created March 14, 2023 12:46
Show Gist options
  • Save shiv122/06e9149c8dea33fbfb407b2755eeae59 to your computer and use it in GitHub Desktop.
Save shiv122/06e9149c8dea33fbfb407b2755eeae59 to your computer and use it in GitHub Desktop.
calculate dice
function calculateDiceProbabilities(history) {
const counts = [0, 0, 0, 0, 0, 0];
// Count the occurrences of each number in the history
for (let i = 0; i < history.length; i++) {
const roll = history[i];
if (roll >= 1 && roll <= 6) {
counts[roll - 1]++;
}
}
const totalRolls = history.length;
const probabilities = counts.map(count => count / totalRolls);
return probabilities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment