成晖的回答:
//首先定义概率数组 $Probability["1-10"]?=?0.6; $Probability["11-50"]?=?0.25; $Probability["51-100"]?=?0.10; $Probability["101-200"]?=?0.05; //扩大1000倍便于计算 foreach($Probability?as?$k?=>?$v){ ???$Probability[$k]?=?$v*1000; } $Num?=?0; $Random?=?rand(1,1000);//生成随机数 foreach($Probability?as?$k?=>?$v){ ???if(??$Num?<?$Random?&&?$Random?<=?$v+$Num){ ??????//进入这里表示随机数在哪一个范围内 ??????$Range?=?explode("-",?$k); ??????//生成范围区间的随机数 ??????$Result?=?rand($Range[0],$Range[1]); ??????echo?$Result; ??????break; ???}else{ ??????$Num?+=?$v; ???} }
何嘉乐的回答: