Node.JS 中基于概率的函数
Probability based function in Node.JS
我想创建一个功能,当点击打开箱子时,这个人会得到一个随机角色,基于他们每个 class:
的概率
1 - 常见:90%
2 - 稀有:8%
3 - 超级稀有:2%
这在 Node.JS 中可行吗?
这确实是可能的,而且在任何语言中都是可能的,因为这只是生成一个随机数(参见 Math.Random())并做一些数学运算!
下面是一个代码注释示例,用于解释正在发生的事情:
// The actual function to draw a character class
function drawCharacterClass() {
// We generate a random number between 0 and 1
const r = Math.random();
// If the number is between 0 and 0.02 (included) - so 2% chance
if (r <= 0.02)
return "Super Rare";
// If the number is between 0.02 (not included) and 0.10 - so 8% chance.
if (r <= 0.10)
return "Rare";
// If the number is anything else, so between 0.10 (not included) and 1 - so 90% chance.
else
return "Common";
}
// ===== ===== ===== Testing code below ===== ===== =====
// Now let's make a function that draw a lot of characters and record how many of each we got, this way we can make sure the drawCharacterClass() function is correct
function checkProbabilities(sample) {
const res = {};
for (let i = 0; i < sample; ++i) {
const c = drawCharacterClass();
res[c] = ++res[c] || 0;
}
return res;
}
// Let's poll 10 million characters.
// The higher the number the more precise it will be, but also slower.
const records = checkProbabilities(10000000);
// We count the total. We could also just use 10000000 directly.
const total = records["Common"] + records["Rare"] + records["Super Rare"];
// Now for each class of character, we count the amount we got from this class (records[class]) divided by the total of characters drawn. This will tell us what % of this class of character got drawn among the 10 millions draws.
console.log(`${Math.round(records["Common"] / total * 100)}% of Common.`); // Should say 90%
console.log(`${Math.round(records["Rare"] / total * 100)}% of Rare.`); // Should say 8%
console.log(`${Math.round(records["Super Rare"] / total * 100)}% of Super Rare.`); // Should say 2%
您可以执行这段代码,将使用代码段顶部的函数模拟绘制 1000 万个字符(可能需要几秒钟),然后计算每个字符的有效掉落率 class,让您确保它们是正确的。
请注意,这个功能drawCharacterClass()
非常简单,只尊重你在问题中所说的内容。它的目标只是向您展示“是的,这是可能的”,但到目前为止 不是最好的方法 ,特别是如果您打算更改百分比、 classes,添加另一个 class,等等...
在这种情况下,您最好采用更 modular/generic 的方法,例如,您只需更改如下内容:
const classes = {
"Common": 0.90,
"Rare": 0.08,
"Super Rare": 0.02
}
另请注意,它只会绘制某个 class 字符,而不是字符本身。
但是既然你知道如何从其他物品中随机选择一个物品(这里是几个 class 中的一个 class),那么从几个角色中选择一个角色应该不是很难!
我想创建一个功能,当点击打开箱子时,这个人会得到一个随机角色,基于他们每个 class:
的概率1 - 常见:90% 2 - 稀有:8% 3 - 超级稀有:2%
这在 Node.JS 中可行吗?
这确实是可能的,而且在任何语言中都是可能的,因为这只是生成一个随机数(参见 Math.Random())并做一些数学运算!
下面是一个代码注释示例,用于解释正在发生的事情:
// The actual function to draw a character class
function drawCharacterClass() {
// We generate a random number between 0 and 1
const r = Math.random();
// If the number is between 0 and 0.02 (included) - so 2% chance
if (r <= 0.02)
return "Super Rare";
// If the number is between 0.02 (not included) and 0.10 - so 8% chance.
if (r <= 0.10)
return "Rare";
// If the number is anything else, so between 0.10 (not included) and 1 - so 90% chance.
else
return "Common";
}
// ===== ===== ===== Testing code below ===== ===== =====
// Now let's make a function that draw a lot of characters and record how many of each we got, this way we can make sure the drawCharacterClass() function is correct
function checkProbabilities(sample) {
const res = {};
for (let i = 0; i < sample; ++i) {
const c = drawCharacterClass();
res[c] = ++res[c] || 0;
}
return res;
}
// Let's poll 10 million characters.
// The higher the number the more precise it will be, but also slower.
const records = checkProbabilities(10000000);
// We count the total. We could also just use 10000000 directly.
const total = records["Common"] + records["Rare"] + records["Super Rare"];
// Now for each class of character, we count the amount we got from this class (records[class]) divided by the total of characters drawn. This will tell us what % of this class of character got drawn among the 10 millions draws.
console.log(`${Math.round(records["Common"] / total * 100)}% of Common.`); // Should say 90%
console.log(`${Math.round(records["Rare"] / total * 100)}% of Rare.`); // Should say 8%
console.log(`${Math.round(records["Super Rare"] / total * 100)}% of Super Rare.`); // Should say 2%
您可以执行这段代码,将使用代码段顶部的函数模拟绘制 1000 万个字符(可能需要几秒钟),然后计算每个字符的有效掉落率 class,让您确保它们是正确的。
请注意,这个功能drawCharacterClass()
非常简单,只尊重你在问题中所说的内容。它的目标只是向您展示“是的,这是可能的”,但到目前为止 不是最好的方法 ,特别是如果您打算更改百分比、 classes,添加另一个 class,等等...
在这种情况下,您最好采用更 modular/generic 的方法,例如,您只需更改如下内容:
const classes = {
"Common": 0.90,
"Rare": 0.08,
"Super Rare": 0.02
}
另请注意,它只会绘制某个 class 字符,而不是字符本身。
但是既然你知道如何从其他物品中随机选择一个物品(这里是几个 class 中的一个 class),那么从几个角色中选择一个角色应该不是很难!