3 回答
TA贡献1856条经验 获得超5个赞
你可以试试这种方法,在代码中注释。
只需确保机会的总和低于 100,然后将 0 放入其中,以填补剩余的机会。
与其他方法相比,这使您可以轻松添加/删除稀有度或更改机会,而无需触及所有其他值。
如果您想以 8% 的几率再添加一个,只需在数组上添加
{type: 'oneMore', chance: 8}
工作完成了,一切仍然有效:)
var rarities = [{
type: "common",
chance: 0
}, {
type: "mythics",
chance: 35
}, {
type: "legends",
chance: 20
}, {
type: "ub",
chance: 1
}];
function pickRandom() {
// Calculate chances for common
var filler = 100 - rarities.map(r => r.chance).reduce((sum, current) => sum + current);
if (filler <= 0) {
console.log("chances sum is higher than 100!");
return;
}
// Create an array of 100 elements, based on the chances field
var probability = rarities.map((r, i) => Array(r.chance === 0 ? filler : r.chance).fill(i)).reduce((c, v) => c.concat(v), []);
// Pick one
var pIndex = Math.floor(Math.random() * 100);
var rarity = rarities[probability[pIndex]];
console.log(rarity.type);
}
pickRandom();
pickRandom();
pickRandom();
pickRandom();
pickRandom();
pickRandom();
TA贡献1765条经验 获得超5个赞
您需要总结获得选择类型的正确值的机会。
function getType() {
var gen = Math.floor(Math.random() * 100);
console.log(gen);
if (gen < 2) return 'ub';
if (gen < 5) return 'legends';
if (gen < 13) return 'mythics';
if (gen < 23) return 'galarians';
if (gen < 39) return 'alolans';
return 'common';
}
console.log(getType());
TA贡献2051条经验 获得超10个赞
试试这个代码:
var gen = Math.floor(Math.random() * 100);
var type = common;
if(gen > 59) type = common;
else if(gen < 2) type = ub;
else if(gen < 3) type = legends;
else if(gen < 8) type = mythics;
以较低的开始 if statstatment (<)
添加回答
举报