PHP:如何随机选择最大概率值?

我有以下数组和代码


    $a = [

      149 => 55,

      130 => 10,

      131 => 5,

      132 => 5,

      133 => 10,

      134 => 10,

      135 => 5

    ];


   $rand = rand (0,(count($a)-1));


   echo array_values($a)[$rand];

这将主要给出结果,5,10而不是55.


总价值是 100% 的概率。值可以是十进制的,也可以是 55.55、10.10 等,但总体上将是 100%


我已经关注了https://www.geeksforgeeks.org/how-to-get-random-value-out-of-an-array-in-php/


但这并没有像预期的那样给出完美的结果。


所以应该主要随机选择哪个概率最高。


所以结果可能是这样的:55、55、10、10、10、55、5等。


我发现了一些有用的链接Generate random results by weight in PHP? 其中概率 = 权重


慕姐8265434
浏览 135回答 4
4回答

慕尼黑8549860

这是一个类似于 GA 中轮盘选择的实现。EReload 的答案版本,但以总和而不是 100 为界。&nbsp; &nbsp; $a = [&nbsp; &nbsp; &nbsp; 149 => 55,&nbsp; &nbsp; &nbsp; 130 => 10,&nbsp; &nbsp; &nbsp; 131 => 5,&nbsp; &nbsp; &nbsp; 132 => 5,&nbsp; &nbsp; &nbsp; 133 => 10,&nbsp; &nbsp; &nbsp; 134 => 10,&nbsp; &nbsp; &nbsp; 135 => 5&nbsp; &nbsp; ];&nbsp; &nbsp;echo randSelect($a);&nbsp; &nbsp;function randSelect($a) {&nbsp; &nbsp; &nbsp; &nbsp; $values = array_values($a);&nbsp; &nbsp; &nbsp; &nbsp; $sum = array_sum($values);&nbsp; &nbsp; &nbsp; &nbsp; $rand = (rand(0,1000)/1000) * $sum;&nbsp; &nbsp; &nbsp; &nbsp; $partialSum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for ($i=0; $i < count($values); $i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $partialSum += $values[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($partialSum >= $rand){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $values[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // incase you are using something like array_count_values and are actually looking for the keys&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return array_keys($a)[$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}

喵喔喔

据我了解,无论您的数组中出现多少次较小的数字,您都希望在 rand 方法中更频繁地出现更大的数字。您首先需要独特的阵列。权重随机是一种简单的随机方法,但是您可以通过求和权而不是自身来更自由地控制权重。$a = [&nbsp; &nbsp; &nbsp; 149 => 55,&nbsp; &nbsp; &nbsp; 130 => 10,&nbsp; &nbsp; &nbsp; 131 => 5,&nbsp; &nbsp; &nbsp; 132 => 5,&nbsp; &nbsp; &nbsp; 133 => 10,&nbsp; &nbsp; &nbsp; 134 => 10,&nbsp; &nbsp; &nbsp; 135 => 5&nbsp; &nbsp; ];$val_arr = array_unique(array_values($a));function rand_by_sum($arr, $power=1){&nbsp; &nbsp; &nbsp; &nbsp; $sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; $f_val = function($f)use($power){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return pow($f, $power);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; foreach($arr as $f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sum += $f_val($f);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $rand = mt_rand(0, $sum);&nbsp; &nbsp; &nbsp; &nbsp; $tmp_sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach($arr as $f){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp_sum += $f_val($f);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($tmp_sum >= $rand) return $f;&nbsp; &nbsp; &nbsp; &nbsp; }}for($i=0; $i< 10; $i++){&nbsp; &nbsp; &nbsp; &nbsp; echo rand_by_sum($val_arr, $argv[1]) . " ";}echo "\n";这里有一些不同 pow 的测试结果php test.php 0.555 5 10 55 5 55 55 5 55 55&nbsp;php test.php 255 55 10 55 55 55 55 55 55 55&nbsp;php test.php 155 10 55 55 55 55 55 55 55 10要获取值,您可以还原数组,55 => [149]然后从随机获取结果,然后在还原数组的值中再次随机获取

跃然一笑

我认为你实际上可以打乱数组并弹出一个元素,再次打乱并弹出元素,这将是随机的,那些概率更大的数字将首先出现。您可以做的是创建另一个包含 100 个数字的数组,表示总概率,并在其中插入与其值相等的数字数量,最后您将其打乱以稍后随机选择一个索引。然后你会得到一个包含 100 个数字的数组,其中重复次数最多的数字是最有可能的。最后,您只需要选择一个随机索引并创建您的数组。你能告诉我你是在寻找这样的东西还是我误解了这个问题function getProb($array, $elements){&nbsp; &nbsp; $myNewArray = [];&nbsp; &nbsp; $myProbabilisticArray = $this->getProbabilisticArray($array);&nbsp; &nbsp; for ($i=0; $i < $elements; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; $myNewArray[] = $myProbabilisticArray[array_rand($myProbabilisticArray)];&nbsp; &nbsp; }&nbsp; &nbsp; return $myNewArray;}function getProbabilisticArray($array) {&nbsp; &nbsp; $myNewArray = [];&nbsp; &nbsp; rsort($array);&nbsp; &nbsp; $currentProbability = 0;&nbsp; &nbsp; $accumulatedProbability = $array[0];&nbsp; &nbsp; $currentPosition = 0;&nbsp; &nbsp; while ($currentProbability < 100) {&nbsp; &nbsp; &nbsp; &nbsp; if ($currentProbability > $accumulatedProbability) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $currentPosition++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $accumulatedProbability += $array[$currentPosition];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; array_push($myNewArray, $array[$currentPosition]);&nbsp; &nbsp; &nbsp; &nbsp; $currentProbability++;&nbsp; &nbsp; }&nbsp; &nbsp; shuffle($myNewArray);&nbsp; &nbsp; return $myNewArray;}

慕容708150

现在,你的数组是这样的: -55,&nbsp;10,&nbsp;5,&nbsp;5,&nbsp;10,&nbsp;10,&nbsp;5现在,您应该生成一个介于 [0, 100) 之间的随机数,我们称之为r。现在,如果r介于 [0, 55) 之间,请选择值 55。否则,如果r介于 [55, 55 + 10 = 65) 之间,则选择值 10。否则,如果r介于 [65, 65 + 5 = 70) 之间,则选择值 5。否则,如果r介于 [70, 70 + 5 = 75) 之间,则选择值 5。否则,如果r介于 [75, 75 + 10 = 85) 之间,则选择值 10。否则,如果r介于 [85, 85 + 10 = 95) 之间,则选择值 10。否则,如果r介于 [95, 95 + 5 = 100) 之间,则选择值 5。我相信你会明白的......所以,对于一般情况,如果你有一个名为“arr”的数组,这是伪代码: -function SELECTPROB(){&nbsp; &nbsp; $r = generateRandomNumber(0, 100);&nbsp; &nbsp; //function to generate random number between 0 and 100, (100 exclusive)&nbsp; &nbsp; $sum = 0;&nbsp; &nbsp; foreach($arr as $i)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if($r >= $sum && $r < $sum + $i)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $i&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $sum = $sum + $i&nbsp; &nbsp; }&nbsp; &nbsp; return -1&nbsp; &nbsp; //Should technically never reach upto this, but it can if your probability's sum is not 100}
打开App,查看更多内容
随时随地看视频慕课网APP