基于处理用户和点的整个地图可能有许多类似的方法。这很简单,如果用户不多,也可以正常工作。但是当用户数量增加时,内存甚至 CPU 使用率可能会出现很大的性能问题。所以我考虑了一个可能的解决方案,考虑到性能。
这里描述了我将遵循的根据概率绘制用户的方法:
+------+--------+
| User | Points | Bar graph:
+------+--------+
| A | 20 | |~~~~~~~~~~~~~~~~~~~~|
+------+--------+
| B | 10 | |~~~~~~~~~~|
+------+--------+
| C | 1 | |~|
+------+--------+
| D | 5 | |~~~~~|
+------+--------+
| E | 12 | |~~~~~~~~~~~~|
+------+--------+
| F | 8 | |~~~~~~~~|
+------+--------+
TOTAL | 56 | Random number: 33
+--------+
If we take all bars and put them heel and toe we get something like this:
A B C D E F
|~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~|~|~~~~~|~~~~~~~~~~~~|~~~~~~~~|
Position 33 is up here ▲ so we got a winner: user D
这是一个简单的概念,但它可能会破坏性能,具体取决于算法实现(例如,如果我们尝试按顺序执行)。
我要做的是将用户组一分为二,并将第一部分的点总和与随机数进行比较。如果数量小于第一部分积分(累计)总和,则该数字必须对应该部分内的用户,否则该数字对应第二部分。这种方法必须递归地应用于每个选择的部分,直到我们得到一个用户(获胜者)。这样,我们在第一次迭代中丢弃了 1/2 的用户总数,在第二次迭代中丢弃了 1/4,依此类推。这意味着,如果我们有 100 万用户,那么在第二次迭代中,我们将摆脱 75 万用户。不错的国际海事组织。
这是基于 PHP 和 MySQL 的解决方案...
用户表:
CREATE TABLE `User` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`points` int(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
数据库交互类:
class UserDb
{
private $pdo;
private $sumIntervalStmt;
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
$this->sumIntervalStmt = null;
}
public function getTotal()
{
$query = 'SELECT COUNT(`id`) FROM `User`;';
$result = $this->pdo->query($query);
return (int)($result->fetchColumn());
}
public function getTotalPoints()
{
$query = 'SELECT SUM(`points`) FROM `User`;';
$result = $this->pdo->query($query);
return (int)($result->fetchColumn());
}
public function sumPointsInterval($offset, $length)
{
if ($this->sumIntervalStmt === null) {
$this->sumIntervalStmt = $this->pdo->prepare(
'SELECT SUM(points) FROM (' .
'SELECT points FROM `User` LIMIT ?, ?' .
') AS Subgroup;'
);
}
$this->sumIntervalStmt->bindValue(1, (int)$offset, \PDO::PARAM_INT);
$this->sumIntervalStmt->bindValue(2, (int)$length, \PDO::PARAM_INT);
$this->sumIntervalStmt->execute();
return (int)($this->sumIntervalStmt->fetchColumn());
}
public function getUserByOffset($offset)
{
$query = 'SELECT * FROM `User` LIMIT ?, 1;';
$stmt = $this->pdo->prepare($query);
$stmt->bindValue(1, (int)$offset, \PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchObject();
}
}
用户抽奖等级:
class UserRaffle
{
private $users;
public function __construct(UserDb $users)
{
$this->users = $users;
}
public function drawUser()
{
$total = $this->users->getTotal();
$number = rand(1, $this->users->getTotalPoints());
$offset = 0;
$length = ceil($total / 2);
$count = $total;
$sum = $this->users->sumPointsInterval($offset, $length);
$accum = 0;
while ($count > 1) {
if ($number <= $sum) {
$count -= $count - $length;
$length = ceil($length / 2);
$interval = $this->users->sumPointsInterval($offset, $length);
$sum = $accum + $interval;
} else {
$accum += $sum;
$offset += $length;
$count -= $length;
$length = ceil($count / 2);
$interval = $this->users->sumPointsInterval($offset, $length);
$sum += $interval;
}
}
return $this->users->getUserByOffset($offset);
}
}
和执行:
$pdo = new \PDO('mysql:dbname=test;host=localhost', 'username', '********');
$users = new UserDb($pdo);
$raffle = new UserRaffle($users);
$winner = $raffle->drawUser();