Skip to content

Instantly share code, notes, and snippets.

@niaodan2b
Last active January 11, 2021 08:17
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 niaodan2b/7f3dc1d13b98655b8ca1dd875abb676e to your computer and use it in GitHub Desktop.
Save niaodan2b/7f3dc1d13b98655b8ca1dd875abb676e to your computer and use it in GitHub Desktop.
v2ex盲盒概率
function permutation(a, callback, m) {
let n = a.length;
m = m || n;
function recur(_a, tmpResult = []) {
if (tmpResult.length === m) {
callback(tmpResult);
} else {
for (let i = 0; i < _a.length; i++) {
let tmpA = _a.concat();
let _tmpResult = tmpResult.concat();
_tmpResult.push(tmpA[i]);
tmpA.splice(i, 1);
recur(tmpA, _tmpResult);
}
}
}
recur(a);
}
var artObjects = ["女孩", "男孩", "乳牛", "博客", "甜点", "朋克"]
var impossibleObjects = [
["甜点", "朋克", "女孩"],
["乳牛", "甜点", "博客"],
["女孩", "乳牛", "甜点"],
["甜点", "乳牛", "男孩"],
["博客", "女孩", "男孩"],
["朋克", "男孩", "博客"]
]
var count = {}
artObjects.forEach(x => count[x] = artObjects.map(y => 0))
permutation(artObjects, (tmpResult) => {
if (impossibleObjects.every((x, i) => !x.includes(tmpResult[i]))) {
tmpResult.forEach((x, i) => {
count[x][i] += 1
})
}
})
artObjects.forEach(name => {
const sum = count[name].reduce((a, b) => a + b, 0);
console.log(name, ':', count[name].map(num => Math.round(num * 100 / sum)))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment