PHP example code to achieve winning

小云云
Release: 2023-03-20 17:38:02
Original
1771 people have browsed it

This article mainly shares with you the example code of php to achieve winning, hoping to help everyone.

<?php 
//设置中奖概率


     $prize_arr = array(
  
      0=>array( &#39;id&#39;=>1,&#39;prize&#39;=>&#39;现金500W&#39;,&#39;v&#39;=>1 ), //概率为1/200
      1=>array( &#39;id&#39;=>2,&#39;prize&#39;=>&#39;iphone7&#39;,&#39;v&#39;=>5 ),  
      2=>array( &#39;id&#39;=>3,&#39;prize&#39;=>&#39;耐克跑鞋&#39;,&#39;v&#39;=>10 ),
      3=>array( &#39;id&#39;=>4,&#39;prize&#39;=>&#39;魔声耳机&#39;,&#39;v&#39;=>24 ),
      4=>array( &#39;id&#39;=>5,&#39;prize&#39;=>&#39;蓝牙音响&#39;,&#39;v&#39;=>60 ),
      5=>array( &#39;id&#39;=>6,&#39;prize&#39;=>&#39;现金1元&#39;,&#39;v&#39;=>100000 )
    );
 /*
16  * 对数组进行处理
17  */


foreach( $prize_arr as $k => $v ){
     //使用新数组item
    $item[$v[&#39;id&#39;]] = $v[&#39;v&#39;]; 
    
}
function get_rand($item){
     $num = array_sum($item);//计算出分母200
     foreach( $item as $k => $v ){
       $rand = mt_rand(1, $num);//概率区间(整数) 包括1和200
      /*
43        *这个算法很666 
44        */
      if( $rand <= $v ){
           //循环遍历,当下标$k = 1的时候,只有$rand = 1 才能中奖 
           $result = $k;
          echo $rand.&#39;--&#39;.$v;
          break;
      }else{
           //当下标$k=6的时候,如果$rand>100 必须$rand < = 100 才能中奖 ,那么前面5次循环之后$rand的概率区间= 200-1-5-10-24-60 (1,100) 必中1块钱
          $num-=$v;
          echo &#39;*&#39;.$rand.&#39;*&#39;."&ensp;"."&ensp;"."&ensp;";
       }
     }
     return $result;
 }
 var_dump(get_rand($item));
 ?>
Copy after login

Related recommendations:
Detailed explanation of php winning algorithm

php example of implementing lottery program winning probability algorithm

Analysis of ideas for implementing the winning probability algorithm function in php

The above is the detailed content of PHP example code to achieve winning. For more information, please follow other related articles on the PHP Chinese website!

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