我需要一些我真不知道如何制作它的东西。
以下是一个例子:
我想要的是,用户1,有30%的几率,用户2有10%,用户3有60%。但这些数字也可以是0.01而不是6.现在想要的是,它是随机的,但也有机会的百分比。我真的不知道如何解释它。然后,它为用户3提供60%的机会,用户2的机会为10,用户1的机率为30%。但是,这最后可以尽可能长。这该怎么做?对不起,我的解释真的很糟糕。
谢谢!
答案 0 :(得分:1)
您想要的可以被描述为“加权概率分布”或技术性“discrete distribution”和“Categorical distribution”:
分类分布是一种离散概率分布,它描述了随机变量的可能结果,该随机变量可以承担K个可能的基本事件之一,并且每个基本事件的概率都是单独指定的。
- 维基百科
如果您有random variable with uniform distribution in the range 0 to 1,则可以使用Inverse Method构建所需的任何发布。
您的第一步是规范化分发。这意味着要确保曲线下方的面积等于一(即,你的体重总和不超过100%)。对于离散分布,它意味着确保权重之和等于1。 [这与将值作为向量并在同一方向上计算单位向量相同],只需取每个值并将其除以值的总和。
因此,你可以这样做:
(original)
user 1 = 3
user 2 = 1
user 3 = 6
对此:
sum = 3 + 1 + 6 = 10
(normalized)
user 1 = 3 / 10 = 0.3
user 2 = 1 / 10 = 0.1
user 3 = 6 / 10 = 0.6
接下来,获取累积分布。也就是说,对于每个值,您不需要它(标准化)的权重,而是它的权重加上之前的所有权重。
因此,你可以从这个
(normalized)
user 1 = 0.3
user 2 = 0.1
user 3 = 0.6
对此:
(cumulative)
user 1 = 0.3
user 2 = 0.1 + 0.3 = 0.4
user 3 = 0.6 + 0.3 + 0.1 = 1
最后,您获得random variable with uniform distribution in the range 0 to 1并查看下面的值:
$r = (float)rand()/(float)getrandmax();
if ($r <= 0.3) return "user 1"; // user 1 = 0.3
else if ($r <= 0.4) return "user 2"; // user 2 = 0.4
else return "user 3"; // user 3 = 1
注意:范围是包容性的,因为PHP is weird。
好的,一起去,(丑陋的)PHP:
$p = ['user 1' => 3, 'user 2' => 1, 'user 3' => 6];
$s = array_sum($p);
$n = array_map(function($i) use ($p, $s){return $i/$s;},$p);
$a = []; $t = 0;
foreach($n as $k => $i) {$t += $i; $a[$k] = $t;}
$r = (float)rand()/(float)getrandmax();
foreach($a as $k => $i) { if ($r <= $i) return $k; }
让我们重新实现MySQL,因为原因。
首先我们需要一个包含输入的表,例如:
SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`
然后我们总结值
SELECT sum(chance) FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input
然后我们正常化
SELECT id, chance / sum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input CROSS JOIN (SELECT sum(chance) as sum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input) s
然后我们累积
SELECT id, chance / sum as sum, (@tmp := @tmp + chance / sum) as csum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input CROSS JOIN (SELECT sum(chance) as sum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input) s CROSS JOIN (SELECT @tmp := 0) cheat
然后我们选择
SELECT id from (
SELECT id, chance / sum as sum, (@tmp := @tmp + chance / sum) as csum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input CROSS JOIN (SELECT sum(chance) as sum FROM (SELECT 'user 1' AS `id`, 3 AS `chance`
UNION
SELECT 'user 2' AS `id`, 1 AS `chance`
UNION
SELECT 'user 3' AS `id`, 6 AS `chance`) input) s CROSS JOIN (SELECT @tmp := 0) cheat) a
CROSS JOIN (SELECT RAND() as r) random
WHERE csum > r
LIMIT 1
答案 1 :(得分:0)
编辑:此答案基于标题&#34; PHP ....&#34;不是标签&#34; mysql&#34;
每个用户都有n 门票。在你的情况下,你有10张票,因此需要一个0到9之间的随机数。一个不优雅的解决方案可能是:
<?php
$tickets = array();
$number_of_tickets = 0;
foreach($users as $user) {
for($i = 0; i < $user->tickets; $i++) {
$tickets[] = $user->id;
$tickets++;
}
}
$lucky_draw = rand(0, $number_of_tickets);
$winner = tickets[$lucky_draw] //ID of the user
print("And the winner is...." . $winner);