基于PHP代碼實現(xiàn)中獎概率算法可用于刮刮卡、大轉(zhuǎn)盤等抽獎算法
大轉(zhuǎn)盤中獎概率算法在我們的日常生活中,經(jīng)常遇到,那么基于php代碼是如何實現(xiàn)中獎概率算法的,下面通過一段代碼實例給大家介紹php中獎概率算法,代碼簡單易懂,并且附有注釋,具體代碼如下所示:
<?php
/*
* 經(jīng)典的概率算法,
* $proArr是一個預(yù)先設(shè)置的數(shù)組,
* 假設(shè)數(shù)組為:array(100,200,300,400),
* 開始是從1,1000 這個概率范圍內(nèi)篩選第一個數(shù)是否在他的出現(xiàn)概率范圍之內(nèi),
* 如果不在,則將概率空間,也就是k的值減去剛剛的那個數(shù)字的概率空間,
* 在本例當(dāng)中就是減去100,也就是說第二個數(shù)是在1,900這個范圍內(nèi)篩選的。
* 這樣 篩選到最終,總會有一個數(shù)滿足要求。
* 就相當(dāng)于去一個箱子里摸東西,
* 第一個不是,第二個不是,第三個還不是,那最后一個一定是。
* 這個算法簡單,而且效率非常 高,
* 關(guān)鍵是這個算法已在我們以前的項目中有應(yīng)用,尤其是大數(shù)據(jù)量的項目中效率非常棒。
*/
function get_rand($proArr) {
$result = '';
//概率數(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;
}
/*
* 獎項數(shù)組
* 是一個二維數(shù)組,記錄了所有本次抽獎的獎項信息,
* 其中id表示中獎等級,prize表示獎品,v表示中獎概率。
* 注意其中的v必須為整數(shù),你可以將對應(yīng)的 獎項的v設(shè)置成0,即意味著該獎項抽中的幾率是0,
* 數(shù)組中v的總和(基數(shù)),基數(shù)越大越能體現(xiàn)概率的準(zhǔn)確性。
* 本例中v的總和為100,那么平板電腦對應(yīng)的 中獎概率就是1%,
* 如果v的總和是10000,那中獎概率就是萬分之一了。
*
*/
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'平板電腦','v'=>1),
'1' => array('id'=>2,'prize'=>'數(shù)碼相機(jī)','v'=>5),
'2' => array('id'=>3,'prize'=>'音箱設(shè)備','v'=>10),
'3' => array('id'=>4,'prize'=>'4G優(yōu)盤','v'=>12),
'4' => array('id'=>5,'prize'=>'10Q幣','v'=>22),
'5' => array('id'=>6,'prize'=>'下次沒準(zhǔn)就能中哦','v'=>50),
);
/*
* 每次前端頁面的請求,PHP循環(huán)獎項設(shè)置數(shù)組,
* 通過概率計算函數(shù)get_rand獲取抽中的獎項id。
* 將中獎獎品保存在數(shù)組$res['yes']中,
* 而剩下的未中獎的信息保存在$res['no']中,
* 最后輸出json個數(shù)數(shù)據(jù)給前端頁面。
*/
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid = get_rand($arr); //根據(jù)概率獲取獎項id
$res['yes'] = $prize_arr[$rid-1]['prize']; //中獎項
unset($prize_arr[$rid-1]); //將中獎項從數(shù)組中剔除,剩下未中獎項
shuffle($prize_arr); //打亂數(shù)組順序
for($i=0;$i<count($prize_arr);$i++){
$pr[] = $prize_arr[$i]['prize'];
}
$res['no'] = $pr;
print_r($res);
下面再給大家分享一段實例代碼基于Java實現(xiàn)中獎概率計算
做移動的項目,有個需求,做個搖獎的活動!其中中獎的計算比較惡心,用戶要改動各個獎項的中獎概率,而且每天的獎項有個數(shù)限制。一二三四五六等獎,概率不通,怎么算一個用戶參與了中沒中將呢?苦思了一下,可以用Random類的 nextInt(int x)方法產(chǎn)生一個范圍內(nèi)的隨機(jī)數(shù),產(chǎn)生到那個區(qū)間就是幾等獎了,中獎區(qū)間的產(chǎn)生是動態(tài)的。貼出源代碼,僅供參考!
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);
//計算最小公倍數(shù)
int common = getN(list);
System.out.println("最小公倍數(shù):"+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;//循環(huán)次數(shù)
for(int i = 0;i < times; i++){
int ri = getRandom(common);//產(chǎn)生隨機(jī)數(shù)
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 + " 手機(jī):" + d + " 筆記本電腦:" + e + " 演唱會門票:" + f + " 謝謝參與:" + g);
}
/**
* 求最大公約數(shù)
*/
public static int gcd(int m, int n){
while (true){
if ((m = m % n) == 0)
return n;
if ((n = n % m) == 0)
return m;
}
}
/**
* 求最小公倍數(shù)
*/
public static int gys(int z, int y){
int t = 0;
int c = 0;
c = gcd(z,y);
t = z * y / c;
return t;
}
/**
* 求幾個數(shù)的最小公倍數(shù)
*/
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;
}
/**
* 產(chǎn)生隨機(jī)數(shù)
*/
public static int getRandom(int y){
int result = r.nextInt(y);
return result;
}
}
相關(guān)文章
ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能示例
這篇文章主要介紹了ThinkPHP5.1+Ajax實現(xiàn)的無刷新分頁功能,結(jié)合實例形式詳細(xì)分析了ThinkPHP5.1+Ajax無刷新分頁具體原理、前臺數(shù)據(jù)發(fā)送與后臺處理相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
destoon后臺網(wǎng)站設(shè)置變成空白的解決方法
這篇文章主要介紹了destoon后臺網(wǎng)站設(shè)置變成空白的解決方法,需要的朋友可以參考下2014-06-06

