<?php /* 大致思想就是:将剩余展示数目累加 比如:A:5,B:8,C:10 累加后:A:5,B:13,C:23 然后随即一个数,范围0~23 判断 随机数是否小于A或B或C,小于则成功 效果:谁的剩余展示数目越多,被随机到的几率越高 */ function pc_rand_weighted($numbers) { $total = 0; foreach ( $numbers as $number => $weight ) { $total += $weight; $distribution [$number] = $total; } $rand = mt_rand ( 0, $total - 1 ); foreach ( $distribution as $number => $weights ) { if ($rand < $weights) { return $number; } } } $ads = array ( 'ford' => 12234,//广告客户,剩余展示数目 'att' => 33424, 'ibm' => 16823 ); $ad = pc_rand_weighted ( $ads ); var_dump ( $ad ); ?>