一个或多个数组的加权洗牌?

发布于 2025-01-07 19:17:34 字数 462 浏览 4 评论 0原文

使用嵌套数组中的权重对一个或多个数组进行混洗的好算法是什么?

示例:

$array = array(
  array("name"=>"John", "rank"=>3),
  array("name"=>"Bob", "rank"=>1),
  array("name"=>"Todd", "rank"=>8),
  array("name"=>"Todd", "rank"=>14),
  array("name"=>"Todd", "rank"=>4)
);

我希望数组随机洗牌,但我希望 rank 值是一个权重。因此,那些数字排名较低的人更有可能位于列表的顶部。

我尝试了一些事情,例如迭代数组并提取使用 mt_rand(mt_rand(0,$value),$value) 选择的数组,但我不认为我在正确的轨道...

What is a good algorithm that shuffles an array or arrays using weights from the nested arrays?

Example:

$array = array(
  array("name"=>"John", "rank"=>3),
  array("name"=>"Bob", "rank"=>1),
  array("name"=>"Todd", "rank"=>8),
  array("name"=>"Todd", "rank"=>14),
  array("name"=>"Todd", "rank"=>4)
);

I want the array randomly shuffled but I want the rank value to be a weight. So those with a low number rank are more likely to be at the top of the list.

I've experimented with a few things, like iterating through the array and pulling out arrays chosen using mt_rand(mt_rand(0,$value),$value) but I don't think I'm on the right track...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪我们做朋友吧 2025-01-14 19:17:34

我能够像这样解决这个问题:

function compare($a, $b)
{
  $share_of_a = $a['rank'];
  $share_of_b = $b['rank'];
  return mt_rand(0, ($share_of_a+$share_of_b)) > $share_of_a ? 1 : -1;
}

usort($array, "compare"); // Sort the array using the above compare function when comparing
$array = array_reverse($array);

I was able to solve this problem like so:

function compare($a, $b)
{
  $share_of_a = $a['rank'];
  $share_of_b = $b['rank'];
  return mt_rand(0, ($share_of_a+$share_of_b)) > $share_of_a ? 1 : -1;
}

usort($array, "compare"); // Sort the array using the above compare function when comparing
$array = array_reverse($array);
过气美图社 2025-01-14 19:17:34

你可以尝试这样的事情:

function weightedshuffle ($a, $b) {
    return rand(0, $a['rank'] + $b['rank']) <= $a['rank'];
}

usort($data, 'weightedshuffle');

You can try something like this:

function weightedshuffle ($a, $b) {
    return rand(0, $a['rank'] + $b['rank']) <= $a['rank'];
}

usort($data, 'weightedshuffle');
穿越时光隧道 2025-01-14 19:17:34

由于这个问题首先在谷歌上搜索 php array Weighted shuffle 时出现,并且接受的答案无法解决它 - 这是基于我发现的某些算法的解决方案。

相当快(对于 PHP),概率分布也经过测试并且正确

<?php

/**
 * Input can be specified as basic array or array of arrays:
 * - array = [key1 => weight1, key2 => weight2, ...]
 * - array = [[..., weightKey => weight], [..., weightKey2 => weight2], ...]
 *
 * Usage:
 *
 * $arr = ['key1' => 1, 'key2' => 2, 'key3' => 3];
 * weighted_shuffle($arr);
 *
 * On average key3 is gonna be the first 50% of the time
 *
 * @param array         $array Array to shuffle
 * @param string|null   $weight_key Optional weight key if input is array of arrays 
 */
function weighted_shuffle(array &$array, $weight_key = null)
{
    if($weight_key === null) {
        $arr = $array;
    } else {
        $arr = array_combine(array_keys($array), array_column($array, $weight_key));
    }
    $max = 1.0 / getrandmax();
    array_walk($arr, function (&$v, $k) use($max) {
        $v = pow(rand()*$max, 1.0/$v);
    });
    arsort($arr);
    array_walk($arr, function (&$v, $k) use($array) {
       $v = $array[$k];
    });
    $array = $arr;
}

Since this question is first on google while searching php array weighted shuffle and accepted answer doesn't solve it - here is the solution, based on some algorithm i found.

Pretty fast (for PHP), probability distribution is also tested and correct

<?php

/**
 * Input can be specified as basic array or array of arrays:
 * - array = [key1 => weight1, key2 => weight2, ...]
 * - array = [[..., weightKey => weight], [..., weightKey2 => weight2], ...]
 *
 * Usage:
 *
 * $arr = ['key1' => 1, 'key2' => 2, 'key3' => 3];
 * weighted_shuffle($arr);
 *
 * On average key3 is gonna be the first 50% of the time
 *
 * @param array         $array Array to shuffle
 * @param string|null   $weight_key Optional weight key if input is array of arrays 
 */
function weighted_shuffle(array &$array, $weight_key = null)
{
    if($weight_key === null) {
        $arr = $array;
    } else {
        $arr = array_combine(array_keys($array), array_column($array, $weight_key));
    }
    $max = 1.0 / getrandmax();
    array_walk($arr, function (&$v, $k) use($max) {
        $v = pow(rand()*$max, 1.0/$v);
    });
    arsort($arr);
    array_walk($arr, function (&$v, $k) use($array) {
       $v = $array[$k];
    });
    $array = $arr;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文