概述//获取中间记录的idfunctionget_rand(){$arr=['0'=>['id'=>1,'name'=>'test1','rand'=>0],'1'=>['id'=>2,'name'=>'test2','rand&#
// 获取中间记录的ID
function get_rand() {
$arr = [
\'0\'=>[\'ID\'=>1,\'name\'=>\'test1\',\'rand\'=>0],
\'1\'=>[\'ID\'=>2,\'name\'=>\'test2\',\'rand\'=>1],
\'2\'=>[\'ID\'=>3,\'name\'=>\'test3\',\'rand\'=>2],
\'3\'=>[\'ID\'=>4,\'name\'=>\'test4\',\'rand\'=>1],
\'4\'=>[\'ID\'=>5,\'name\'=>\'test5\',\'rand\'=>2],
\'5\'=>[\'ID\'=>6,\'name\'=>\'test6\',\'rand\'=>6]
];
$result = false;
$all_rand = 0;
foreach ($arr as $v) {
$all_rand +=$v[\'rand\'];
}
foreach ($arr as $key=>$value) {
if ($value[\'rand\']>0) {
$pro = $value[\'rand\'];
$randNum = mt_rand(1,$all_rand);
if ($randNum<=$pro) {
$result = $value[\'ID\'];
break;
} else {
$all_rand -= $pro;
}
}
}
return $result;
}
echo get_rand();
总结
以上是内存溢出为你收集整理的php大转盘算法全部内容,希望文章能够帮你解决php大转盘算法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
© 版权声明
THE END
请登录后查看评论内容