生成随机获胜者并显示获胜几率 - 我这样做对吗?
我正在一个网站上举办竞赛,有 3215 名参赛者有资格获得 5 台索尼 PSP。
我相信计算赔率的公式是totalEntrants - 奖品/奖品:
(3215-5)/5 = 642
所以获胜的赔率是 642 比 1 - 对吗? (我数学很烂)
在我的数据库中包含 3215 行的表中,我会像这样随机选择一行吗?
SELECT * from entries
WHERE entries.won = 0
ORDER BY RAND()
LIMIT 1
现在我有一行,我需要将won
列设置为1
,这样参赛者就不能再次获胜,然后再次运行?这是我第一次这样做,所以我只想确认我是否做得正确。
I'm running a contest on a website and I have 3215 entrants who are eligible for 5x Sony PSPs.
I believe the formula to count the odds is totalEntrants - prizes / prizes:
(3215-5)/5 = 642
so that's an odds of 642 to 1 of winning - is that right? ( I suck at math )
And in my table which contains 3215 rows in the database I would just select a random row like so?
SELECT * from entries
WHERE entries.won = 0
ORDER BY RAND()
LIMIT 1
Now I have one row, and I need to set the won
column to 1
so the entrant can't win again, then run it again? This is my first time doing it so I just want confirmation on if I'm doing it correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是正确的。五人可以获胜,共有 3215 名参赛者,因此获胜的几率为 3215 ÷ 5,即 643 分之一,即 642 比 1。每 643 场胜利就有 1 场,这意味着每 1 场胜利就有 642 场失败。请注意“x in y 机会”与“x-to-y 机会”之间细微的一次性差异。
你的选择方法看起来不错。您还可以通过将其更改为
LIMIT 5
来一次性选择它们。That's right. Five people can win, there are 3215 entrants, so the odds of winning are 3215 ÷ 5 which is 1 in 643, or 642-to-1. 1 in every 643 wins meaning there are 642 losers to every 1 winner. Note the subtle one-off difference between "x in y chance" versus "x-to-y chance".
Your selection method looks fine. You could also select them all at once by changing it to
LIMIT 5
.为什么不直接使用
LIMIT 5
一步选出获胜者呢?Why not just use
LIMIT 5
to select the winners in one step?获胜赔率是总参赛人数/奖品
无需减少奖品数量。例如,如果我们有 2 名参赛者和一个奖品,则赔率是 2/1,这意味着每个参赛者都有两次获胜机会之一(如果我们减少奖品数量,则为 1/1,意味着一对一的机会 - 当然赢...)
查询似乎是正确的:
(假设您有更新的代码
'won' 字段)
Winning odds are totalEntrants / prizes
There is no need to reduce the number of prizes. e.g., if we have 2 entrants and one prize the odds are 2/1, meaning each entrant has one of two chance to win (if we'd reduce the number of prizes it's be 1/1, meaning one to one chance - sure win...)
The query seems right:
(assuming you have code that updates
the 'won' field)
我认为您无法在同一查询中选择和更新。我要做的是使用您的查询来选择随机获胜者,并在返回其 ID 的寄存器上执行更新,以将“won”字段更改为值 1。然后您只需重复该过程 4 次。 :)
I think you can't select and update in the same query. What I would do is to use your query to select a random winner and execute an update on the register whose ID was returned to change the field "won" to have value 1. Then you just repeat the process 4 times. :)
使用:
您也可以使用:
...但性能没有差异 - 请参阅:NOT IN 与 NOT EXISTS 与 LEFT JOIN / IS NULL:MySQL
更新:使用
LIMIT 5
并不理想,因为它不能确保一个人只会获胜一次,除非您假设一个人只进入一次(不太可能)。Use:
You could also use:
...but there's no difference in performance - see: NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: MySQL
UPDATE: Using
LIMIT 5
is not ideal because it doesn't ensure that a person will win only once, unless you assume that a person only entered once (unlikely).