首頁 >後端開發 >php教程 >php 中獎機率演算法

php 中獎機率演算法

WBOY
WBOY原創
2016-08-08 09:24:171021瀏覽

我們先完成後台PHP的流程,PHP的主要工作是負責配置獎項及對應的中獎概率,當前端頁點擊翻動某個方塊時會想後台PHP發送ajax請求,那麼後台PHP根據配置的概率,通過概率演算法給出中獎結果,同時將未中獎的獎項資訊一併以JSON資料格式傳送給前端頁面。

先來看機率計算函數

<span> 1</span> <span>function</span> get_rand(<span>$proArr</span><span>) { 
</span><span> 2</span>     <span>$result</span> = ''<span>; 
</span><span> 3</span>  
<span> 4</span>     <span>//</span><span>概率数组的总概率精度 </span>
<span> 5</span>     <span>$proSum</span> = <span>array_sum</span>(<span>$proArr</span><span>); 
</span><span> 6</span>  
<span> 7</span>     <span>//</span><span>概率数组循环 </span>
<span> 8</span>     <span>foreach</span> (<span>$proArr</span> <span>as</span> <span>$key</span> => <span>$proCur</span><span>) { 
</span><span> 9</span>         <span>$randNum</span> = <span>mt_rand</span>(1, <span>$proSum</span><span>); 
</span><span>10</span>         <span>if</span> (<span>$randNum</span> <= <span>$proCur</span><span>) { 
</span><span>11</span>             <span>$result</span> = <span>$key</span><span>; 
</span><span>12</span>             <span>break</span><span>; 
</span><span>13</span>         } <span>else</span><span> { 
</span><span>14</span>             <span>$proSum</span> -= <span>$proCur</span><span>; 
</span><span>15</span> <span>        } 
</span><span>16</span> <span>    } 
</span><span>17</span>     <span>unset</span> (<span>$proArr</span><span>); 
</span><span>18</span>  
<span>19</span>     <span>return</span> <span>$result</span><span>; 
</span><span>20</span> } 

上述程式碼是一段經典的機率演算法,$proArr是一個預先設定的數組,假設數組為:array(100,200,300,400),開始是從1,1000這個機率範圍內篩選第一個數是否在他的出現機率範圍之內, 如果不在,則將機率空間,也就是k的值減去剛剛的那個數字的機率空間,在本例當中就是減去100,也就是說第二個數是在1,900這個範圍內篩選的。這樣篩選到最終,總會有一個數滿足要求。就等於去一個箱子裡摸東西,第一個不是,第二個不是,第三個還不是,那最後一個一定是。這個演算法簡單,而且效率非常高,關鍵在於這個演算法已在我們先前的專案中有應用,尤其是大數據量的專案中效率非常棒。

接下來我們通過PHP配置獎。

<span>1</span> <span>$prize_arr</span> = <span>array</span><span>( 
</span><span>2</span>     '0' => <span>array</span>('id'=>1,'prize'=>'平板电脑','v'=>1), 
<span>3</span>     '1' => <span>array</span>('id'=>2,'prize'=>'数码相机','v'=>5), 
<span>4</span>     '2' => <span>array</span>('id'=>3,'prize'=>'音箱设备','v'=>10), 
<span>5</span>     '3' => <span>array</span>('id'=>4,'prize'=>'4G优盘','v'=>12), 
<span>6</span>     '4' => <span>array</span>('id'=>5,'prize'=>'10Q币','v'=>22), 
<span>7</span>     '5' => <span>array</span>('id'=>6,'prize'=>'下次没准就能中哦','v'=>50), 
<span>8</span> ); 

該二維數組,記錄了所有本次抽獎的獎項信息,其中id表示中獎等級,prize表示獎品,v表示中獎概率。注意其中的v必須為整數,你可以將對應的獎項的v設定成0,即意味著該獎項抽中的幾率是0,數組中v的總和(基數),基數越大越能體現機率的準確性。本例中v的總和為100,那麼平板電腦對應的中獎機率就是1%,如果v的總和是10000,那中獎機率就是萬分之一了。

每次前端頁面的請求,PHP循環獎項設定數組,透過機率計算函數get_rand獲取抽中的獎項id。將中獎獎品保存在數組$res['yes']中,而剩下的未中獎的資訊保存在$res['no']中,最後輸出json個數資料給前端頁面。

<span> 1</span> <span>foreach</span> (<span>$prize_arr</span> <span>as</span> <span>$key</span> => <span>$val</span><span>) { 
</span><span> 2</span>     <span>$arr</span>[<span>$val</span>['id']] = <span>$val</span>['v'<span>]; 
</span><span> 3</span> <span>} 
</span><span> 4</span>  
<span> 5</span> <span>$rid</span> = get_rand(<span>$arr</span>); <span>//</span><span>根据概率获取奖项id </span>
<span> 6</span>  
<span> 7</span> <span>$res</span>['yes'] = <span>$prize_arr</span>[<span>$rid</span>-1]['prize']; <span>//</span><span>中奖项 </span>
<span> 8</span> <span>unset</span>(<span>$prize_arr</span>[<span>$rid</span>-1]); <span>//</span><span>将中奖项从数组中剔除,剩下未中奖项 </span>
<span> 9</span> <span>shuffle</span>(<span>$prize_arr</span>); <span>//</span><span>打乱数组顺序 </span>
<span>10</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$prize_arr</span>);<span>$i</span>++<span>){ 
</span><span>11</span>     <span>$pr</span>[] = <span>$prize_arr</span>[<span>$i</span>]['prize'<span>]; 
</span><span>12</span> <span>} 
</span><span>13</span> <span>$res</span>['no'] = <span>$pr</span><span>; 
</span><span>14</span> <span>echo</span> json_encode(<span>$res</span>);

以上就介紹了php 中獎機率演算法,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn