且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从列表中选择&LT N个随机元素; T>在C#中

更新时间:2022-11-22 11:06:58

遍历,并为每个元素进行选择的概率=(需要数/数​​左)

Iterate through and for each element make the probability of selection = (number needed)/(number left)

所以,如果你有40个项目,首先将有机会5/40被选中的。如果是,下一个具有4/39机会,否则它有一个5/39机会。到时候你到了最后,你将有你的5个项目,往往你必须所有的人都在那之前。

So if you had 40 items, the first would have a 5/40 chance of being selected. If it is, the next has a 4/39 chance, otherwise it has a 5/39 chance. By the time you get to the end you will have your 5 items, and often you'll have all of them before that.