根据设置的概率选择一个用户 [英] Select a user according to set probability

查看:55
本文介绍了根据设置的概率选择一个用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库存储有关用户,他们的组和关系的信息。用户表中的一列 fcount 跟踪每个用户在当前组中具有的关系数;它从0开始,在适当的时候我将其递增。

我需要编写一个脚本,该脚本选择给定组中的所有用户,然后随机选择其中一个用户,其可能性取决于一个人的关系数;关系越少,就意味着概率越大。

My database stores info about users, their groups, and relationships. One of the columns, fcount, in the users table tracks the number of relationships each user has had within their current group; it starts at 0, and I increment it when appropriate.
I need to write a script that selects all users in a given group and then randomly selects one of them with the probability of being selected being based on the number of relationships one has had; fewer relationships means a greater probability.

当前,我用以下代码实现了这一点,减去了概率部分:

Currently, I accomplish this, minus the probability part, with this code:

$query = "SELECT uid FROM users WHERE groupid=:gid AND status='1'";
...
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    $friends[] = $row[0];
}

foreach ($users as $value) {
    $key = array_search($value, $friends);
    unset($friends[$key]);
    shuffle($friends);
    $newrel = end($friends);
    $user = $value;
    $friends[] = $value;

    $query = "UPDATE users SET rel=:newrel WHERE uid=:uid";
    $query_params = array(':newrel' => $newrel, ':uid' => $user );
... }

我认为调整概率的最简单方法是编辑 $ friends 数组,以使用户多次出现。因此, fcount 为0的用户将在数组中重复5次,而 fcount 的用户为1重复3次。 [也许这不是处理它的最佳方法,但这对我来说很有意义,并且与我已有的代码相适应。您可以自由地提供更好的方案。]

I am thinking that the easiest way to adjust probability would be to edit the $friends array so that users show up more than once. So, users with an fcount of 0 will be repeated in the array 5 times, and users with an fcount of 1 are repeated 3 times. [Maybe this isn't the best way to handle it, but it makes sense to me and fits with the code I already have. You're free to offer a better scheme.]

我还没弄清楚的是如何获取用户数组并乘以用户条目

What I haven't managed to figure out is how to take the array of users and multiply the entries of the users who ought to be multiplied.

SELECT uid, fcount FROM users WHERE groupid=:gid AND status='1';

返回反映此表的数组:

+-----+--------+
| uid | fcount |
+-----+--------+
| 105 |      3 |
| 106 |      2 |
| 107 |      0 |
| 108 |      0 |
| 109 |      1 |
+-----+--------+

其中然后变成这样的数组:

which then turns into an array like this:

array(15) { 
[0]=> string(3) "105" 
[1]=> string(3) "106" 
[2]=> string(3) "107" 
[3]=> string(3) "107" 
[4]=> string(3) "107" 
[5]=> string(3) "107" 
[6]=> string(3) "107" 
[7]=> string(3) "108" 
[8]=> string(3) "108" 
[9]=> string(3) "108" 
[10]=> string(3) "108" 
[11]=> string(3) "108"
[12]=> string(3) "109" 
[13]=> string(3) "109" 
[14]=> string(3) "109" 
}  






在我看来,这可以通过 foreach 来实现,该方法将每个用户id n 次推入新数组,具体取决于循环。即使可能会失败,我也会尝试构造它。


It seems to me that this could be accomplished with a foreach that pushes each userid n times into a new array according to if statements in the loop. I'll try to construct it, even though I'm likely to fail.

推荐答案

$problist = array();
foreach ($row as $value) {
    if ($value['fcount'] == 0) {
        array_push($problist, $value['uid'], $value['uid'], $value['uid'], $value['uid'], $value['uid']);
    } elseif ($value['fcount'] == 1) {
        array_push($problist, $value['uid'], $value['uid'], $value['uid']);
    } elseif ($value['fcount'] >= 2) {
        $problist[] = $value['uid'];
    }
}

这篇关于根据设置的概率选择一个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆