发现网上发的基本上都是不完整的,所以贡献一下下,分享给大家。
完整的代码下载:http://yun.baidu.com/s/1i3FXbPr
- /**
- * $proArr = array('1'=>'概率', '2'=>'概率');
- * $proCount = array('1'=>'库存', '2'=>'库存');
- */
- function getRand($proArr, $proCount) {
- $result = '';
- $proSum = 0;
- foreach ($proCount as $key => $val) {
- if ($val <= 0) {
- continue;
- } else {
- $proSum = $proSum $proArr[$key];
- }
- }
- foreach ($proArr as $key => $proCur) {
- if ($proCount[$key] <= 0) {
- continue;
- } else {
- $randNum = mt_rand(1, $proSum);
- if ($randNum <= $proCur) {
- $result = $key;
- break;
- } else {
- $proSum -= $proCur;
- }
- }
- }
- unset($proArr);
- return $result;
- }
复制代码
|