以x%的概率执行某段代码
100%
if( true )
1/2
if( rand(0,1) === 0 )
1/3
if( rand(0,2) === 0 )
1/4
if( rand(0,3) === 0 )
1/x
if( rand(0,x) === 0 )
x%
if( rand(0,99) < x )
x‰
if( rand(0,999) < x )
x‱
if( rand(0,9999) < x )
| 1 | /** |
| 2 | * 以$x/$y的概率返回true |
| 3 | * |
| 4 | * @param int $x |
| 5 | * @param int $y |
| 6 | * @return bool |
| 7 | * |
| 8 | * prob(1,2) = 1/2 |
| 9 | * prob(1,100) = 1/100 |
| 10 | * |
| 11 | */ |
| 12 | function prob($x, $y){ |
| 13 | return ( mt_rand(0, $y-1) < $x ); |
| 14 | } |

浙公网安备 33010602011771号