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--;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀