码迷,mamicode.com
首页 > Web开发 > 详细

PHP游戏概率方法

时间:2018-05-17 13:41:39      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:func   random   amp   tmp   rate   pre   ota   bre   style   

<?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游戏概率方法

标签:func   random   amp   tmp   rate   pre   ota   bre   style   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!