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

Home php教程 php手冊 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
/*
 * 經典的概率算法,
 * $proArr是一個預先設置的數組,
 * 假設數組為:array(100,200,300,400),
 * 開始是從1,1000 這個概率范圍內篩選第一個數是否在他的出現概率范圍之內, 
 * 如果不在,則將概率空間,也就是k的值減去剛剛的那個數字的概率空間,
 * 在本例當中就是減去100,也就是說第二個數是在1,900這個范圍內篩選的。
 * 這樣 篩選到最終,總會有一個數滿足要求。
 * 就相當于去一個箱子里摸東西,
 * 第一個不是,第二個不是,第三個還不是,那最后一個一定是。
 * 這個算法簡單,而且效率非常高,
 * 這個算法在大數據量的項目中效率非常棒。
 */
function get_rand($proArr) { 
  $result = &#39;&#39;; 
  //概率數組的總概率精度 
  $proSum = array_sum($proArr); 
  //概率數組循環(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; 
} 
 
 
/*
 * 獎項數組
 * 是一個二維數組,記錄了所有本次抽獎的獎項信息,
 * 其中id表示中獎等級,prize表示獎品,v表示中獎概率。
 * 注意其中的v必須為整數,你可以將對應的 獎項的v設置成0,即意味著該獎項抽中的幾率是0,
 * 數組中v的總和(基數),基數越大越能體現概率的準確性。
 * 本例中v的總和為100,那么平板電腦對應的 中獎概率就是1%,
 * 如果v的總和是10000,那中獎概率就是萬分之一了。
 * 
 */
$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;數碼相機&#39;,&#39;v&#39;=>5), 
  &#39;2&#39; => array(&#39;id&#39;=>3,&#39;prize&#39;=>&#39;音箱設備&#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;下次沒準就能中哦&#39;,&#39;v&#39;=>50), 
); 
 
/*
 * 每次前端頁面的請求,PHP循環(huán)獎項設置數組,
 * 通過概率計算函數get_rand獲取抽中的獎項id。
 * 將中獎獎品保存在數組$res[&#39;yes&#39;]中,
 * 而剩下的未中獎的信息保存在$res[&#39;no&#39;]中,
 * 最后輸出json個數數據給前端頁面。
 */
foreach ($prize_arr as $key => $val) { 
  $arr[$val[&#39;id&#39;]] = $val[&#39;v&#39;]; 
} 
$rid = get_rand($arr); //根據概率獲取獎項id 
 
$res[&#39;yes&#39;] = $prize_arr[$rid-1][&#39;prize&#39;]; //中獎項 
unset($prize_arr[$rid-1]); //將中獎項從數組中剔除,剩下未中獎項 
shuffle($prize_arr); //打亂數組順序 
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)