国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 PHP lottery probability algorithm

PHP lottery probability algorithm

Nov 11, 2016 am 09:16 AM

/* 
 * 不同概率的抽獎(jiǎng)原理就是把0到*(比重總數(shù))的區(qū)間分塊
 * 分塊的依據(jù)是物品占整個(gè)的比重,再根據(jù)隨機(jī)數(shù)種子來(lái)產(chǎn)生0-* 中的某個(gè)數(shù)
 * 判斷這個(gè)數(shù)是落在哪個(gè)區(qū)間上,區(qū)間對(duì)應(yīng)的就是抽到的那個(gè)物品。
 * 隨機(jī)數(shù)理論上是概率均等的,那么相應(yīng)的區(qū)間所含數(shù)的多少就體現(xiàn)了抽獎(jiǎng)物品概率的不同。
 */  function get_rand($proArr) {   
    $result = array();    foreach ($proArr as $key => $val) { 
        $arr[$key] = $val['v']; 
    }  
    $proSum = array_sum($arr);      // 計(jì)算總權(quán)重
    $randNum = mt_rand(1, $proSum);    $d1 = 0;    $d2 = 0;    for ($i=0; $i < count($arr); $i++)
    {        $d2 += $arr[$i];        if($i==0)
        {            $d1 = 0;
        }        else
        {            $d1 += $arr[$i-1];
        }        if($randNum >= $d1 && $randNum <= $d2)
        {            $result = $proArr[$i];
        }
    }    unset ($arr); 
    return $result;
} /* 
 * 使用較多的為這個(gè)方法
 */ function get_rand1($proArr) {   
    $result = array();    foreach ($proArr as $key => $val) { 
        $arr[$key] = $val[&#39;v&#39;]; 
    } 
    // 概率數(shù)組的總概率  
    $proSum = array_sum($arr);        
    asort($arr);    // 概率數(shù)組循環(huán)   
    foreach ($arr as $k => $v) {   
        $randNum = mt_rand(1, $proSum);   
        if ($randNum <= $v) {   
            $result = $proArr[$k];   
            break;   
        } else {   
            $proSum -= $v;   
        }         
    }     
    return $result;   
}
/*
 * 獎(jiǎng)項(xiàng)數(shù)組 
 * 獎(jiǎng)品id,名稱(chēng),比重 
 */$arr = array(   
    array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;特等獎(jiǎng)&#39;,&#39;v&#39;=>1),    array(&#39;id&#39;=>2,&#39;name&#39;=>&#39;一等獎(jiǎng)&#39;,&#39;v&#39;=>5),    array(&#39;id&#39;=>3,&#39;name&#39;=>&#39;二等獎(jiǎng)&#39;,&#39;v&#39;=>10),    array(&#39;id&#39;=>4,&#39;name&#39;=>&#39;三等獎(jiǎng)&#39;,&#39;v&#39;=>12),    array(&#39;id&#39;=>5,&#39;name&#39;=>&#39;四等獎(jiǎng)&#39;,&#39;v&#39;=>22),    array(&#39;id&#39;=>6,&#39;name&#39;=>&#39;沒(méi)中獎(jiǎng)&#39;,&#39;v&#39;=>50)
);   
測(cè)試結(jié)果(10000次):
get_rand():
    count_1:0 count_2:490 count_3:1021 count_4:1172 count_5:2172 count_6:5145
    特等獎(jiǎng)中獎(jiǎng)率全為:0get_rand1():
    count_1:92 count_2:477 count_3:1017 count_4:1195 count_5:2264 count_6:4955總體感覺(jué) get_rand1() 更準(zhǔn)確些......

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)