【问题标题】:Change from MySQL Rand() to PHP random_int从 MySQL Rand() 更改为 PHP random_int
【发布时间】:2017-01-27 07:05:06
【问题描述】:

我们正在创建一个随机数游戏。我们目前在以下语句中使用 MySQL Rand() 函数:

private function doPreEventStart($user) {
$row = db_fetch_item("SELECT resultid FROM ResultPackage 
    where ResultPackage.slotid like '%{$this->curSlotId}'
    and ResultPackage.PackageID like '%{$user->packageid}%'
    ORDER BY RAND() LIMIT 1");
$this->curResultId = $row['resultid'];
}

我们需要将其更改为 PHP 函数 Random_Int。请有人可以通过更改上述代码来建议一种方法。从 SQL 语句中的 WHERE 条件可以看出,随机数仍然需要在一定范围内。

【问题讨论】:

  • 发帖前有没有做研究?
  • 目前的方法有什么问题?
  • 当前方法实际上并不是随机的。 “RAND() 并不是一个完美的随机生成器。它是一种按需生成随机数的快速方法,可以在同一 MySQL 版本的平台之间移植。”根据我们的研究,我们发现 Random_int 是一种更好的方法

标签: php mysql random


【解决方案1】:

我正在测试这段代码:

private function doPreEventStart($user) {
    $row = db_fetch_item("SELECT resultid as MaxResult FROM ResultPackage 
        where ResultPackage.slotid like '%{$this->curSlotId}'
        and ResultPackage.PackageID like '%{$user->packageid}%'
        ORDER BY resultid desc LIMIT 1");
    $this->MaxResult = $row['MaxResult'];


    $row = db_fetch_item("SELECT resultid as MinResult FROM ResultPackage 
        where ResultPackage.slotid like '%{$this->curSlotId}'
        and ResultPackage.PackageID like '%{$user->packageid}%'
        ORDER BY resultid asc LIMIT 1");
    $this->MinResult = $row['MinResult'];

    $this->curResultId = int rand ( int $MinResult , int $MaxResult )
    }

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 2012-11-07
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多