Last active
May 12, 2020 11:55
-
-
Save rjyo/b413dce192df06e91e5618ee8d0e0fca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 一次挑选的不重复几率 | |
* | |
* @param n 总种类数 | |
* @param m 第m次挑选 | |
* @param batch 每次选的本数,默认为3 | |
*/ | |
const op = (n: number, m = 0, batch = 3) => { | |
const s = n - batch * m | |
return (s / n) * ((s - 1) / (n - 1)) * ((s - 2) / (n - 2)) | |
} | |
/** | |
* 挑选m次后的不重复几率 | |
* | |
* @param n 总种类数 | |
* @param m 挑选次数 | |
*/ | |
const f = (n: number, m: number) => { | |
let rv = 1 | |
for (let i = 0; i < m; i++) rv *= op(n, i) | |
return rv | |
} | |
console.log(f(100, 1)) // 1 | |
console.log(f(5000, 12)) // 0.8877207442529191 | |
console.log(f(1000, 12)) // 0.5479560754243109 | |
console.log(f(500, 12)) // 0.2955830495139548 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment