英文:
Function for calculating dice odds when rolling multiple die
问题
我正在努力开发一种显示骰子统计信息的方法,就像您可以在网站https://anydice.com/上看到的那样。我已经花了一些时间浏览源代码,但总体来说它相当复杂,所以我决定在这里提问。基本上,我只需要帮助制作一个函数,该函数可以:
- 允许您选择一个具有特定面数的骰子,例如:6。
- 允许您选择要掷这个骰子的次数。
- 返回使用该骰子掷出每个可能数字的百分比。
我知道这个问题可能有点烂,但这是我最后的办法。
到目前为止,我尝试过找到函数,并偶然发现了这篇中等博客,但我想知道是否可以使用百分比来完成这个任务。
英文:
I'm working on a way to display dice statistics like you can see at the site https://anydice.com/ , I've spent a bit looking through the source code but it's pretty thick alltogether so i decided to ask here. Basically, all i need help with is making a function that:
- Lets you pick a dice with a certain amount sides, for example: 6.
- Lets you pick how many times you want to roll this dice.
- Returns the percentages of rolling each possible number with said dice.
I know the question might be a bit shitty, but this is kind of my last resort.
So far, I've tried finding the functions and stumbled upon this medium blog however I was wondering if it could maybe be done with percentages.
答案1
得分: 1
以下是代码的翻译部分:
// 两个六面骰子和一个八面骰子
const dice = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 7, 8],
];
// 获取两个数组的所有组合的函数
function cartesianProduct(a, b) {
return a.flatMap(c => b.map(d => [c, d].flat()));
}
// 获取两个数字的和的函数
function sum(a, b) { return a + b; }
// 所有骰子的所有组合
const allPossibleRolls = dice.reduce(cartesianProduct);
// 每组骰子的和
const sums = allPossibleRolls.map(rolls => rolls.reduce(sum));
// 统计每个和出现的次数
const counts = sums.reduce((acc, n) => Object.assign(acc, {
[n]: (acc[n] || 0) + 1
}), {});
// 将每个计数转换为百分比,除以 allPossibleRolls 的长度
const percents = Object.fromEntries(Object.entries(counts).map(
([sum, count]) => [sum, count / allPossibleRolls.length]));
Object.entries(percents).forEach(([k,v]) => {
console.log(`${k} = ${(v * 100).toFixed(5)}%`);
});
这是您提供的代码的翻译部分。如果您有任何其他问题或需要进一步帮助,请随时提出。
英文:
Here's a way.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Two 6-sided dice, one 8-sided
const dice = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 7, 8],
];
// Function to get all combinations of two arrays
function cartesianProduct(a, b) {
return a.flatMap(c => b.map(d => [c, d].flat()));
}
// Function to get sum of two numbers
function sum(a, b) { return a + b; }
// All combinations of all dice
const allPossibleRolls = dice.reduce(cartesianProduct);
// Sum for each set of rolls
const sums = allPossibleRolls.map(rolls => rolls.reduce(sum));
// Count how many times each sum appears
const counts = sums.reduce((acc, n) => Object.assign(acc, {
[n]: (acc[n] || 0) + 1
}), {});
// Convert each count into a percent by dividing by length of allPossibleRolls
const percents = Object.fromEntries(Object.entries(counts).map(
([sum, count]) => [sum, count / allPossibleRolls.length]));
Object.entries(percents).forEach(([k,v]) => {
console.log(${k} = ${(v * 100).toFixed(5)}%
);
});
<!-- end snippet -->
Doesn't dedupe equivalent rolls like the Medium post you linked to mentions, so the rolls [1, 2] and [2, 1] and treated as separate possibilites. Not sure if that throws off the math. But this returns the same answer as AnyDice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论