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

Home php教程 php手冊(cè) PHP lottery probability algorithm (scratch card, big wheel)_php skills

PHP lottery probability algorithm (scratch card, big wheel)_php skills

Jul 06, 2016 pm 02:24 PM

This article mainly introduces the php winning probability algorithm in detail, which can be used for scratch cards, big wheel and other lottery algorithms. Interested friends can refer to it

The examples in this article are shared with you. PHP winning probability algorithm can be used for scratch cards, big wheel and other lottery algorithms. It is very simple to use. There are detailed comments in the code for your reference. The specific content is as follows

<?php
/*
 * 經(jīng)典的概率算法,
 * $proArr是一個(gè)預(yù)先設(shè)置的數(shù)組,
 * 假設(shè)數(shù)組為:array(100,200,300,400),
 * 開(kāi)始是從1,1000 這個(gè)概率范圍內(nèi)篩選第一個(gè)數(shù)是否在他的出現(xiàn)概率范圍之內(nèi), 
 * 如果不在,則將概率空間,也就是k的值減去剛剛的那個(gè)數(shù)字的概率空間,
 * 在本例當(dāng)中就是減去100,也就是說(shuō)第二個(gè)數(shù)是在1,900這個(gè)范圍內(nèi)篩選的。
 * 這樣 篩選到最終,總會(huì)有一個(gè)數(shù)滿足要求。
 * 就相當(dāng)于去一個(gè)箱子里摸東西,
 * 第一個(gè)不是,第二個(gè)不是,第三個(gè)還不是,那最后一個(gè)一定是。
 * 這個(gè)算法簡(jiǎn)單,而且效率非常高,
 * 這個(gè)算法在大數(shù)據(jù)量的項(xiàng)目中效率非常棒。
 */
function get_rand($proArr) { 
  $result = &#39;&#39;; 
  //概率數(shù)組的總概率精度 
  $proSum = array_sum($proArr); 
  //概率數(shù)組循環(huán) 
  foreach ($proArr as $key => $proCur) { 
    $randNum = mt_rand(1, $proSum); 
    if ($randNum <= $proCur) { 
      $result = $key; 
      break; 
    } else { 
      $proSum -= $proCur; 
    }    
  } 
  unset ($proArr); 
  return $result; 
} 
 
 
/*
 * 獎(jiǎng)項(xiàng)數(shù)組
 * 是一個(gè)二維數(shù)組,記錄了所有本次抽獎(jiǎng)的獎(jiǎng)項(xiàng)信息,
 * 其中id表示中獎(jiǎng)等級(jí),prize表示獎(jiǎng)品,v表示中獎(jiǎng)概率。
 * 注意其中的v必須為整數(shù),你可以將對(duì)應(yīng)的 獎(jiǎng)項(xiàng)的v設(shè)置成0,即意味著該獎(jiǎng)項(xiàng)抽中的幾率是0,
 * 數(shù)組中v的總和(基數(shù)),基數(shù)越大越能體現(xiàn)概率的準(zhǔn)確性。
 * 本例中v的總和為100,那么平板電腦對(duì)應(yīng)的 中獎(jiǎng)概率就是1%,
 * 如果v的總和是10000,那中獎(jiǎng)概率就是萬(wàn)分之一了。
 * 
 */
$prize_arr = array( 
  &#39;0&#39; => array(&#39;id&#39;=>1,&#39;prize&#39;=>&#39;平板電腦&#39;,&#39;v&#39;=>1), 
  &#39;1&#39; => array(&#39;id&#39;=>2,&#39;prize&#39;=>&#39;數(shù)碼相機(jī)&#39;,&#39;v&#39;=>5), 
  &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱設(shè)備&#39;,&#39;v&#39;=>10), 
  &#39;3&#39; => array(&#39;id&#39;=>4,&#39;prize&#39;=>&#39;4G優(yōu)盤&#39;,&#39;v&#39;=>12), 
  &#39;4&#39; => array(&#39;id&#39;=>5,&#39;prize&#39;=>&#39;10Q幣&#39;,&#39;v&#39;=>22), 
  &#39;5&#39; => array(&#39;id&#39;=>6,&#39;prize&#39;=>&#39;下次沒(méi)準(zhǔn)就能中哦&#39;,&#39;v&#39;=>50), 
); 
 
/*
 * 每次前端頁(yè)面的請(qǐng)求,PHP循環(huán)獎(jiǎng)項(xiàng)設(shè)置數(shù)組,
 * 通過(guò)概率計(jì)算函數(shù)get_rand獲取抽中的獎(jiǎng)項(xiàng)id。
 * 將中獎(jiǎng)獎(jiǎng)品保存在數(shù)組$res[&#39;yes&#39;]中,
 * 而剩下的未中獎(jiǎng)的信息保存在$res[&#39;no&#39;]中,
 * 最后輸出json個(gè)數(shù)數(shù)據(jù)給前端頁(yè)面。
 */
foreach ($prize_arr as $key => $val) { 
  $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 
$rid = get_rand($arr); //根據(jù)概率獲取獎(jiǎng)項(xiàng)id 
 
$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中獎(jiǎng)項(xiàng) 
unset($prize_arr[$rid-1]); //將中獎(jiǎng)項(xiàng)從數(shù)組中剔除,剩下未中獎(jiǎng)項(xiàng) 
shuffle($prize_arr); //打亂數(shù)組順序 
for($i=0;$i<count($prize_arr);$i++){ 
  $pr[] = $prize_arr[$i][&#39;prize&#39;]; 
} 
$res[&#39;no&#39;] = $pr; 
print_r($res); 
?>

The above is the entire content of the PHP lottery probability algorithm. I hope it will be helpful to everyone in learning PHP programming. I also hope that everyone will support Script Home.

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)