生成随机获胜者并显示获胜几率 - 我这样做对吗?

发布于 2024-08-09 19:51:35 字数 395 浏览 3 评论 0原文

我正在一个网站上举办竞赛,有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-08-16 19:51:35

这是正确的。五人可以获胜,共有 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.

娜些时光,永不杰束 2024-08-16 19:51:35

为什么不直接使用 LIMIT 5 一步选出获胜者呢?

Why not just use LIMIT 5 to select the winners in one step?

浪菊怪哟 2024-08-16 19:51:35

获胜赔率是总参赛人数/奖品

无需减少奖品数量。例如,如果我们有 2 名参赛者和一个奖品,则赔率是 2/1,这意味着每个参赛者都有两次获胜机会之一(如果我们减少奖品数量,则为 1/1,意味着一对一的机会 - 当然赢...)

查询似乎是正确的:

  • 同一参赛者不会被选中两次
    (假设您有更新的代码
    'won' 字段)
  • 将通过 RANDOM 随机选择用户,每次查询时生成不同的顺序

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:

  • Same entrant won't get selected twice
    (assuming you have code that updates
    the 'won' field)
  • User will be selected randomly by RANDOM generating different order each time querying
¢蛋碎的人ぎ生 2024-08-16 19:51:35

我认为您无法在同一查询中选择和更新。我要做的是使用您的查询来选择随机获胜者,并在返回其 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. :)

粉红×色少女 2024-08-16 19:51:35

使用:

CREATE TEMPORARY TABLE won LIKE ENTRIES;

DECLARE numAwards INT DEFAULT 5;

WHILE numAwards > 0 DO

  INSERT INTO won
    (columns...)
    SELECT e.*
      FROM ENTRIES e
     WHERE e.userid NOT IN (SELECT w.userid FROM won)
  ORDER BY RAND()
     LIMIT 1;

  SET numAwards = numAwards - 1;

END WHILE;

您也可以使用:

   SELECT e.*
     FROM ENTRIES e
LEFT JOIN won w ON w.userid = e.userid
    WHERE w.userid IS NULL
  ORDER BY RAND()
     LIMIT 1;

...但性能没有差异 - 请参阅:NOT IN 与 NOT EXISTS 与 LEFT JOIN / IS NULL:MySQL

更新:使用 LIMIT 5 并不理想,因为它不能确保一个人只会获胜一次,除非您假设一个人只进入一次(不太可能)。

Use:

CREATE TEMPORARY TABLE won LIKE ENTRIES;

DECLARE numAwards INT DEFAULT 5;

WHILE numAwards > 0 DO

  INSERT INTO won
    (columns...)
    SELECT e.*
      FROM ENTRIES e
     WHERE e.userid NOT IN (SELECT w.userid FROM won)
  ORDER BY RAND()
     LIMIT 1;

  SET numAwards = numAwards - 1;

END WHILE;

You could also use:

   SELECT e.*
     FROM ENTRIES e
LEFT JOIN won w ON w.userid = e.userid
    WHERE w.userid IS NULL
  ORDER BY RAND()
     LIMIT 1;

...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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文