gpt4 book ai didi

mysql - Php 随机化的概率为 %

转载 作者:行者123 更新时间:2023-11-29 10:30:40 26 4
gpt4 key购买 nike

我需要一些我真的不知道如何制作的东西。

这是一个例子:

  • 用户 1 = 3
  • 用户 2 = 1
  • 用户 3 = 6

我想要的是,用户 1 有 30% 的机会,用户 2 有 10%,用户 3 有 60%。但这些数字也可以是 0.01,而不是 6。现在想要的是,它是随机的,但也有机会百分比。我真的不知道该如何解释。然后,它以 60% 的机会为用户 3 抽出一个号码,为用户 2 抽出 10% 的机会,为用户 1 抽出 30% 的机会。但是,最后的时间可以尽可能长。这个怎么做?抱歉,我实在不好解释。

谢谢!

最佳答案

您想要的可以描述为“加权概率分布”或技术上的“discrete distribution”和“Categorical distribution”:

A categorical distribution is a discrete probability distribution that describes the possible results of a random variable that can take on one of K possible elementary events, with the probability of each elementary event separately specified.

-- 维基百科

假设您有 random variable with uniform distribution in the range 0 to 1您可以使用 Inverse Method 构建任何您想要的发行版.

第一步是标准化分布。这意味着要确保曲线下方的面积等于 1(即您的体重总和不超过 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; }

Try online .

<小时/>

由于原因,让我们在 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

Try online .

关于mysql - Php 随机化的概率为 %,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com