Home  >  Article  >  Backend Development  >  跪求一 选择 算法。

跪求一 选择 算法。

WBOY
WBOYOriginal
2016-06-23 14:02:12958browse

现在有1个二维数组  包含50个一维数组

每个数组内 有  (质量度系数  0 - 9)  这个元素

如何在选择50个数组之一的同时  保证利用质量度来决定选取该数组的频率

要求: 

50个数组 均有选中的机会


回复讨论(解决方案)

我理解你是这个意思

$ar = array(1,1,2,3,2,5,6,8,3,5,9,2,3,1,4,5);$t = array();foreach($ar as $k=>$v) $t = array_merge($t, array_fill(0, $v, $k));//$t 中保存的是 $ar 的下标srand(20); //固定的种子可使结果重复$res = array_fill(0, count($ar), 0);for($i=0; $i<10000; $i++) {  shuffle($t); //随机打乱  $res[$t[0]]++;}print_r($res);
10000 次中 $ar 各元素被选中的次数
Array
(
    [0] => 173
    [1] => 168
    [2] => 294
    [3] => 480
    [4] => 364
    [5] => 892
    [6] => 989
    [7] => 1385
    [8] => 475
    [9] => 843
    [10] => 1504
    [11] => 300
    [12] => 476
    [13] => 194
    [14] => 664
    [15] => 799
)

于是变成了 质量度有多高 就让他重复多少次 最后随机选取时出现的几率就大了这一个朴素的事实? 没有公式 ,也不用神马公式


就这思路吧  给分了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:正则表达式难题Next article:为甚麽这个变量为null