doulubashang6936 2012-04-01 08:08
浏览 24
已采纳

如何根据元素中存储的概率%编写一个返回元素子集的函数?

I have the following array

$arr = array(
    "person1" => 10,
    "person2" => 10,
    "person3" => 20,
    "person4" => 25,
    "person5" => 35,                    
);

I would like to write a function that takes $arr as an argument and returns 3 elements of the array based on the values stored in each element.

For example if the returned subset yields

$newArr = array(
    "person5" => 35,  
    "person1" => 10,
    "person4" => 25,                  
);

There was a 35% chance that person5 would be the first element stored in $newArr based on the value stored in $arr['person5'] divided by the sum of values stored in the remaining elements. $arr['person5']/($arr['person5'] + $arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])

There was a ~15% chance that person1 would be the second element stored in $newArr based on the value stored in $arr['person1'] divided by the sum of values stored in the remaining elements. $arr['person1']/($arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])

There was a ~45% chance that person4 would be the second element stored in $newArr based on the value stored in $arr['person4'] divided by the sum of values stored in the remaining elements. $arr['person4']/($arr['person4'] + $arr['person3'] + $arr['person2'])

How could I write a function that does this?

  • 写回答

1条回答 默认 最新

  • douwei3172 2012-04-01 08:12
    关注

    You're looking for a roulette wheel selection algorithm, see: http://en.wikipedia.org/wiki/Fitness_proportionate_selection

    $count = 3;
    $arr = array(
        "person1" => 10,
        "person2" => 10,
        "person3" => 20,
        "person4" => 25,
        "person5" => 35,                    
    );
    
    $result = array();
    
    // sort from low to high
    asort($arr);
    
    // loop 3 times (based on count)
    while ($count > 0){
    
        // get the sum of all persons
        $sum = 0;
        foreach ($arr as $rank){
            $sum += $rank;
        }
    
        // get a random value between 0 and sum
        $delta = rand(0, $sum);
        $current = 0;
    
        // keep looping over each item, increasing rank untill $sum has surpassed delta
        // see each item as as person containing a portion of the slice. The bigger the value, the greater the change of being selected
        $current = 0;
        foreach ($arr as $name => $rank){
            $current += $rank;
    
            if ($delta <= $current){
                $result[$name] = $rank;
                break;
            }
        }
    
        unset($arr[$name]);
    
        $count--;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来