php大转盘算法
2021/6/15 12:25:05
本文主要是介绍php大转盘算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// 获取中间记录的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大转盘算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-05易优新手phpStudy本地安装教程-icode9专业技术文章分享
- 2024-07-03php客户端微信小程序根据分享携带参数进入指定页面-icode9专业技术文章分享
- 2024-07-02phpstrom链接ftp 提示上传成功 但是ftp代码没变-icode9专业技术文章分享
- 2024-06-30百度空间安装易优,如何去除URL中的index.php-icode9专业技术文章分享
- 2024-06-30宝塔Linux服务器隐藏index.php-icode9专业技术文章分享
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享