国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

基于PHP代碼實現中獎概率算法可用于刮刮卡、大轉盤等抽獎算法

2024-05-04 23:41:09
字體:
來源:轉載
供稿:網友
大轉盤中獎概率算法在我們的日常生活中,經常遇到,那么基于php代碼是如何實現中獎概率算法的,下面通過一段代碼實例給大家介紹php中獎概率算法
 

大轉盤中獎概率算法在我們的日常生活中,經常遇到,那么基于php代碼是如何實現中獎概率算法的,下面通過一段代碼實例給大家介紹php中獎概率算法,代碼簡單易懂,并且附有注釋,具體代碼如下所示:

<?php/* * 經典的概率算法, * $proArr是一個預先設置的數組, * 假設數組為:array(100,200,300,400), * 開始是從1,1000 這個概率范圍內篩選第一個數是否在他的出現概率范圍之內,  * 如果不在,則將概率空間,也就是k的值減去剛剛的那個數字的概率空間, * 在本例當中就是減去100,也就是說第二個數是在1,900這個范圍內篩選的。 * 這樣 篩選到最終,總會有一個數滿足要求。 * 就相當于去一個箱子里摸東西, * 第一個不是,第二個不是,第三個還不是,那最后一個一定是。 * 這個算法簡單,而且效率非常 高, * 關鍵是這個算法已在我們以前的項目中有應用,尤其是大數據量的項目中效率非常棒。 */function get_rand($proArr) {  $result = '';  //概率數組的總概率精度  $proSum = array_sum($proArr);  //概率數組循環  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(  '0' => array('id'=>1,'prize'=>'平板電腦','v'=>1),  '1' => array('id'=>2,'prize'=>'數碼相機','v'=>5),  '2' => array('id'=>3,'prize'=>'音箱設備','v'=>10),  '3' => array('id'=>4,'prize'=>'4G優盤','v'=>12),  '4' => array('id'=>5,'prize'=>'10Q幣','v'=>22),  '5' => array('id'=>6,'prize'=>'下次沒準就能中哦','v'=>50), ); /* * 每次前端頁面的請求,PHP循環獎項設置數組, * 通過概率計算函數get_rand獲取抽中的獎項id。 * 將中獎獎品保存在數組$res['yes']中, * 而剩下的未中獎的信息保存在$res['no']中, * 最后輸出json個數數據給前端頁面。 */foreach ($prize_arr as $key => $val) {  $arr[$val['id']] = $val['v']; } $rid = get_rand($arr); //根據概率獲取獎項id $res['yes'] = $prize_arr[$rid-1]['prize']; //中獎項 unset($prize_arr[$rid-1]); //將中獎項從數組中剔除,剩下未中獎項 shuffle($prize_arr); //打亂數組順序 for($i=0;$i<count($prize_arr);$i++){  $pr[] = $prize_arr[$i]['prize']; } $res['no'] = $pr; print_r($res); 

下面再給大家分享一段實例代碼基于Java實現中獎概率計算

 做移動的項目,有個需求,做個搖獎的活動!其中中獎的計算比較惡心,用戶要改動各個獎項的中獎概率,而且每天的獎項有個數限制。一二三四五六等獎,概率不通,怎么算一個用戶參與了中沒中將呢?苦思了一下,可以用Random類的 nextInt(int x)方法產生一個范圍內的隨機數,產生到那個區間就是幾等獎了,中獎區間的產生是動態的。貼出源代碼,僅供參考!

package Mzone;import java.util.ArrayList;import java.util.Random;public class Mzone { /** * CopyRright(c)2009-04:  * Project:  * Module ID:  * Comments: 概率計算 * JDK version used: <JDK1.4> * Author:ch * Create Date:2009-04-20 * Modified By:  * Modified Date:  * Why & What is modified  * Version: 1.0 */  static Random r = new Random(); public static void main(String[] args) {  //各個獎項的中獎概率的分母  Integer _5m = new Integer(5);  Integer _500m = new Integer(30);  Integer _ipod = new Integer(500);  Integer _phone = new Integer(1000);  Integer _notebook = new Integer(1500);  Integer _jay = new Integer(50);  ArrayList list = new ArrayList();  if(_5m.intValue()!=0)   list.add(_5m);  if(_500m.intValue()!=0)   list.add(_500m);  if(_ipod.intValue()!=0)   list.add(_ipod);  if(_phone.intValue()!=0)   list.add(_phone);  if(_notebook.intValue()!=0)   list.add(_notebook);  if(_jay.intValue()!=0)   list.add(_jay);  //計算最小公倍數  int common = getN(list);  System.out.println("最小公倍數:"+common);  int a = 0;int b = 0;int c = 0;int d = 0;int e = 0;int f = 0;int g = 0;  int first = 0;int second = 0;int third = 0;int four = 0;int fifth = 0;int sixth = 0;  if(_5m.intValue()!=0){   first = common/_5m.intValue();  }  if(_500m.intValue()!=0){   second = first + (common/_500m.intValue());  }else second = first;  if(_ipod.intValue()!=0){   third = second + (common/_ipod.intValue());  }else third = second;  if(_phone.intValue()!=0){   four = third + (common/_phone.intValue());  }else four = third;  if(_notebook.intValue()!=0){   fifth = four + (common/_notebook.intValue());  }else fifth = four;  if(_jay.intValue()!=0){   sixth = fifth + (common/_jay.intValue());  }else sixth = fifth;  int times = 30000;//循環次數  for(int i = 0;i < times; i++){   int ri = getRandom(common);//產生隨機數   if(ri >= 0 && ri < first){    a++;   }else if(ri >= first && ri < second){    b++;   }else if(ri >= second && ri < third){    c++;   }else if(ri >= third && ri < four){    d++;   }else if(ri >= four && ri < fifth){    e++;   }else if(ri >= fifth && ri < sixth){    f++;   }else{    g++;   }  }  System.out.println("5m值:" + a + " 500m值:" + b + " ipodMP3:" + c + " 手機:" + d + " 筆記本電腦:" + e + " 演唱會門票:" + f + " 謝謝參與:" + g); } /**  * 求最大公約數 */ public static int gcd(int m, int n){  while (true){   if ((m = m % n) == 0)   return n;   if ((n = n % m) == 0)   return m;  } } /** * 求最小公倍數 */ public static int gys(int z, int y){  int t = 0;  int c = 0;  c = gcd(z,y);  t = z * y / c;  return t; } /**  * 求幾個數的最小公倍數 */ public static int getN(ArrayList list){  int t = 1;  for(int i = 0;i<list.size();i++){   Integer temp = (Integer)list.get(i);   t = gys(t,temp.intValue());  }  return t;  } /**  * 產生隨機數 */ public static int getRandom(int y){  int result = r.nextInt(y);  return result; }}


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宽城| 镇远县| 松阳县| 双流县| 盐山县| 克拉玛依市| 太仆寺旗| 冕宁县| 新巴尔虎右旗| 兴文县| 兴海县| 乡宁县| 衢州市| 兰考县| 高青县| 浦东新区| 巴南区| 温宿县| 凤庆县| 邹平县| 扬中市| 酒泉市| 建瓯市| 昭苏县| 巢湖市| 泸水县| 丰宁| 吐鲁番市| 台安县| 仪征市| 利川市| 濮阳县| 郸城县| 古丈县| 瑞昌市| 阿合奇县| 馆陶县| 新丰县| 盐亭县| 台东市| 收藏|