大炮捕鱼

<?php
    /**
    大炮从1-7级。鱼分大,中,小
    7级炮打在 大中小鱼可以捕获成功的概率是 0.01 0.1 1
    n级炮打在 大中小鱼可以捕获成功的概率是7级炮的 n/7
    **/
    function p($n,$base){
        if($n==7){
            return $base;
        }
        return ($n*$base)/7;
    }

    function hit($n,$type){
        if($type=='s'){
            if($n==7){
                return 1;
            }
            $p = (int)round(p($n,1)*1000);
        }
        if($type=='m'){
            $p = (int)round(p($n,0.1)*1000);
        }
        if($type=='b'){
            $p = (int)round(p($n,0.01)*1000);
        }
        if(!isset($p)){
            return 0;
        }    
        $random = rand(1,1000);
        if($random <$p){
            return 1;    
        }
        return 0;
    }

    $hit_count=0;
    $n=10000;
    //测试1000次
    for($i=0;$i<$n;$i++){
        if(hit(6,'m')){
            $hit_count++;
        }
    }    
    $std = round(p(6,0.1)*$n);
    echo '测试'.$n.'次应该击中次数'.$std.' 生成击中次数 '.$hit_count.'<br>';
    
?>
原文地址:https://www.cnblogs.com/23lalala/p/2748769.html