[英]Best way to handle a lottery draw?
我有一张叫做tickets
的表,里面存储了所有购买的门票。 看起来像:
id int AI,primary
userid int
现在,用户显然可以购买任意数量的门票,以增加他们的获胜几率。 现在只运行这个语句有什么问题吗
SELECT
u.id AS winner FROM tickets t
LEFT JOIN users u ON t.user = u.id
ORDER BY RAND() LIMIT 1
对于这样的系统,MySQL 函数是否“真正”足够随机?
似乎有时当我运行查询时,同一个人被选中了很多。
http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.
RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.
另请注意,当您的表变大时,此查询将变得非常昂贵,因为它必须在返回之前对表中的每一行进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.