选择概率随机条目(Select random entry with probability)
假设我将以下信息存储在数据库中:
User Points A 2000 B 1000
我想根据积分数随机选择一个胜利者。 在这种情况下,由于总共有3000分,'A'有可能被选中67%而'B'有33%的可能性。
使用PHP选择获胜者的最有效方法是什么(从计算概率到选择获胜者)? 请注意,播放的用户数量不固定,并且可以达到很大的范围(因此它应该计算'每个用户'而不是修复A和B)。
我一直在玩潜在的解决方案,但尚未弄明白。 我很想听听你的解决方案!
Say I have the following information stored in a database:
User Points A 2000 B 1000
I want to select a winner at random with a probability based on the amount of points. In this case, since there's a total of 3000 points, 'A' has a chance of 67% to be selected and 'B' has a 33% chance.
What is the most efficient way to select the winner using PHP (from calculating probabilities to selecting the winner)? Note that the amount of users playing is not fixed and can range up to a large amount (so it should calculate 'each user' rather than fix on A and B).
I've been playing around with potential solutions but have not yet figured it out. I'd love to hear your solution!
原文:https://stackoverflow.com/questions/13455525