Home > Backend Development > PHP Problem > How to implement the php lucky wheel code

How to implement the php lucky wheel code

藏色散人
Release: 2023-03-14 12:52:01
Original
3408 people have browsed it

php How to implement the Lucky Wheel: 1. Create a PHP sample file; 2. Implement the Lucky Wheel lottery algorithm through the code "function get_gift(){...}".

How to implement the php lucky wheel code

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to implement the php lucky wheel code?

PHP implements the big carousel lottery algorithm

Code:

 function get_gift(){  
        //拼装奖项数组 
        // 奖项id,奖品,概率
        $prize_arr = array(   
          '0' => array('id'=>1,'prize'=>'平板电脑','v'=>0),   
          '1' => array('id'=>2,'prize'=>'数码相机','v'=>1),   
          '2' => array('id'=>3,'prize'=>'音箱设备','v'=>1),   
          '3' => array('id'=>4,'prize'=>'4G优盘','v'=>1),   
          '4' => array('id'=>5,'prize'=>'10Q币','v'=>1),   
          '5' => array('id'=>6,'prize'=>'空奖','v'=>6),   
        );   
        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][&#39;prize&#39;];  //未中奖项数组 
        }   
        $res[&#39;no&#39;] = $pr; 
        // var_dump($res);
 
          
        if($res[&#39;yes&#39;]!=&#39;空奖&#39;){  
            $result[&#39;status&#39;]=1;  
            $result[&#39;name&#39;]=$res[&#39;yes&#39;];  
        }else{  
            $result[&#39;status&#39;]=-1;  
            $result[&#39;msg&#39;]=$res[&#39;yes&#39;];  
        }   
        //return $result;  
        var_dump($result);
    }  
 
    //计算中奖概率
    function get_rand($proArr) {   
      $result = &#39;&#39;;   
      //概率数组的总概率精度   
      $proSum = array_sum($proArr);   
      // var_dump($proSum);
      //概率数组循环   
      foreach ($proArr as $key => $proCur) {   
        $randNum = mt_rand(1, $proSum);  //返回随机整数 
 
        if ($randNum <= $proCur) {   
          $result = $key;   
          break;   
        } else {   
          $proSum -= $proCur;   
        }   
      }   
      unset ($proArr);   
      return $result;   
    }
    get_gift();
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement the php lucky wheel code. 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template