选择抽奖券获奖者 [英] Select raffle ticket winners

查看:65
本文介绍了选择抽奖券获奖者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个脚本,用户可以通过完成任务来获得积分,然后获得的积分越多,赢得奖品的机会就越高. 就像抽奖券比赛一样.

I've created a script where users can get points by completing tasks and then the more points they get the higher chance they have of winning the prize. It works like a raffle ticket competition.

我的问题是产生赢家.

我可以选择这样的代码作为随机赢家:

I can choose a random winner with a code like this:

    function selectWinners($count){
    $select = mysql_query("SELECT first_name from `users` ORDER BY RAND() LIMIT {$count}");
    $results = mysql_fetch_assoc($select);
    echo "Winner is {$results[first_name]}";
    }

但是,这完全是随机的,对于用户获得多少分根本无关紧要.

But this is just completely random and doesn't matter at all as to how many points the users have.

我相信我必须创建一个临时表来显示用户拥有的每个点的ID,然后使用类似的脚本来选择一个随机ID?

I believe I've got to create a temporary table that shows the users ID for every point they have, then use a similar script to select a random ID?

我不确定,只是一个猜测,我也不知道如何编码.

I'm not entire sure, just a guess, I also don't have a clue how that would be coded.

谢谢

推荐答案

您要问的实际上是统计问题,而不是编程问题.

What you're asking is really more a statistics question than a programming one.

您希望每个用户都有机会赢得与其所输入的条目(点")成正比的数字.在现实世界中,这相当于将每个用户的名字每获得一个点就戴一次帽子,然后随机绘制一个名字.单张图纸指定获胜者.

You want each user to have a chance to win proportional to the number of entries they made ("points"). In the real world, this is the equivalent of putting each user's name into a hat once for each point they get, then drawing one name at random. The single drawing names the winner.

通过编程,您可以通过在0和所有用户所获得的总积分减去1之间选择一个随机数(如果需要,可以从1开始.但是计算机使用0,通常会更容易).然后,您遍历用户,将每个点的总和相加.当运行计数超过您选择的随机数时,您当前所在的用户将获胜.

Programmatically, you do this by choosing a random number between zero and the total number of points awarded across all users minus one (you could, if you wish, start at one. But computers use zero and that's generally easier). Then you iterate through the users, adding each one's point total to a running sum. When the running count exceeds the random number you've chosen, the user you're currently on has won.

假设您有三个进入者,分别是Joe,Bob和Alice:

Imagine you have three entrants, named Joe, Bob, and Alice:

Name  Points
------------
Joe   3
Bob   8
Alice 2

名称的顺序无关紧要,概率将以任何方式起作用.

It doesn't matter what order the names are in, the probabilities will work anyhow.

您选择一个介于零和12之间(含12和3)的随机数(12 = 3 + 8 + 2-1).在0-2,乔赢(2 = 3-1).在3-10上,鲍勃获胜(10 = 8 + 3-1).在11-12上,爱丽丝获胜(12 = 3 + 8 + 2-1).请注意,每个范围内包含的值数等于点数-因此,选择给定个体的几率是(个人的点数/总点数),这是您想要的逻辑.

You select a random number between zero and 12 inclusive (12=3+8+2-1). On a 0-2, Joe wins (2=3-1). On a 3-10, Bob wins (10=8+3-1). On an 11-12, Alice wins (12=3+8+2-1). Note that the number of values included in each range is equal to the number of points - thus, the odds of a given individual being selected are (individual's points / total points), the logic you desire.

您检查获胜者的方法如上所述,即从零开始求和,然后加3.如果3> randomvalue,则乔获胜.然后添加8.如果新的11大于随机值,则Bob获胜.然后添加2.现在该数字大于您的随机值(因为13大于您可以生成的最大值).

The way you check the winner is as I said above - start a sum at zero, then add 3. If 3 > randomvalue, Joe wins. Then add 8 to that. If the new 11 is greater than the random value, Bob wins. Then add 2. Now the number is greater than your random value (since 13 is higher than the max you could generate).

从理论上讲,这可以在数据库中完成-但这实际上并不是一个适当的任务.这是应用程序逻辑,我建议在PHP中执行(SQL只是所有用户,他们的积分,甚至总积分的基本SELECT).

This could, in theory, be done in a database - but it's not really an appropriate task there. This is application logic, and I'd recommend doing it in PHP (the SQL would just be a basic SELECT of all the users, their points, and perhaps the point total).

这篇关于选择抽奖券获奖者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆