Home > Backend Development > PHP Tutorial > PHP winning probability algorithm

PHP winning probability algorithm

WBOY
Release: 2016-08-08 09:24:17
Original
1012 people have browsed it

We first complete the backend PHP process. The main job of PHP is to configure the awards and the corresponding winning probabilities. When the front-end page clicks to flip a certain box, it will send an ajax request to the backend PHP. Then the backend PHP will pass the probability based on the configured probability. The algorithm gives the winning result and at the same time sends the unwon prize information to the front-end page in JSON data format.

Let’s look at the probability calculation function first

<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> } 
Copy after login

The above code is a classic probability algorithm. $proArr is a preset array. Assume that the array is: array(100,200,300,400). It starts by screening whether the first number appears in the probability range of 1,1000. Within the probability range, if not, then the probability space, that is, the value of k minus the probability space of the number just now, in this case is minus 100, which means that the second number is 1,900 Filtered within range. In this way, until the end, there will always be a number that meets the requirements. It's like touching something in a box. If the first one isn't right, the second one isn't right, and the third one isn't right, then the last one must be. This algorithm is simple and very efficient. The key is that this algorithm has been applied in our previous projects, especially in projects with large amounts of data. The efficiency is very good.

Next we configure the awards through 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> ); 
Copy after login

This two-dimensional array records all the prize information of this lottery, where id represents the winning level, prize represents the prize, and v represents the probability of winning. Note that v must be an integer. You can set v of the corresponding award to 0, which means that the probability of winning the award is 0. The sum of v in the array (base). The larger the base, the more accurate the probability can be reflected. . In this example, the sum of v is 100, then the probability of winning for the tablet is 1%. If the sum of v is 10,000, the probability of winning is one in ten thousand.

Every time the front-end page is requested, PHP loops through the award setting array, and obtains the drawn award ID through the probability calculation function get_rand. Save the winning prizes in the array $res['yes'], and save the remaining non-winning information in $res['no'], and finally output the json number data to the front-end page.

<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>);
Copy after login

The above introduces the PHP lottery probability algorithm, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template