首页 > Web开发 > 详细

PHP游戏概率方法

时间:2018-05-17 13:07:51      阅读:191      评论:0      收藏:0      [点我收藏+]
<?php

 function createRandomKey($randArr, $rateKey){
        $total = 0;
        $chooseArr = array();
        $pow = 0;
        foreach($randArr as $k => $v){
            $tmp = explode(‘.‘, strval($v[$rateKey]));
            if($tmp[1] && strlen($tmp[1]) > $pow){
                $pow = strlen($tmp[1]);
            }
        }
        $r = pow(10, $pow);
        foreach($randArr as $key => $value){
            $total = $total + $value[$rateKey] * $r;
            $chooseArr[$key] = $total;
        };
        $rand = rand(1, $total);
        $bofore = 0;
        foreach ($chooseArr as $k => $v){
            $v = $v * $r;
            if($rand > $before && $rand <= $v){
               $chooseIndex = $k;
                break;
            }
            $before = $v;
        }
        return $chooseIndex;
    }
    
    $randArr = [
            0 => [‘point‘ => 15,‘rate‘ => 0],
            1 => [‘point‘ => 5,‘rate‘ => 0],
            2 => [‘point‘ => 10,‘rate‘ => 0],
            3 => [‘point‘ => 11,‘rate‘ => 0],
            4 => [‘point‘ => 13,‘rate‘ => 10],
            5 => [‘point‘ => 20,‘rate‘ => 80],
            6 => [‘point‘ => 50,‘rate‘ => 0],
            7 => [‘point‘ => 80,‘rate‘ => 0],
            8 => [‘point‘ => 10,‘rate‘ => 0],
            9 => [‘point‘ => 60,‘rate‘ => 10],
        
        ];
    
    $a = createRandomKey($randArr,$rate = ‘rate‘);
    var_dump($a);
    var_dump($randArr[$a][‘point‘]);

  

技术分享图片

PHP游戏概率方法

原文:https://www.cnblogs.com/qhorse/p/9050312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!