PHP implements custom winning and probability algorithm

I recently played "QQ Speed ​​Mobile Games", and I came out with a coupon A car, but I needed to use lottery tickets to draw a lottery. I was very excited, so I drew a few times.

Similar probabilistic operations have also been involved in the project in the past few days, so I thought about it and simply packaged a probability algorithm implementation class that is accurate to 1/10,000.

<?php

/**
 * Created by PhpStorm.
 * User: hgq
 * Date: 2018/05/07
 * Time: 10:00 am
 * A lottery category, accurate to one ten thousandth
 * Three steps: 1. Accept an array of winning probability; 2. Accept a lottery seed; 3. Return to the winning level
 */
class LuckyDraw {
    /**
     * Array of winning probability, automatically determine the number of prizes
     * The sum of the array key value is 10000, the probability of not winning is automatically calculated, if the initial value exceeds 10000, an error will be thrown
     * @var array
     */
    protected $_rate = array();

    /**
     * Set the probability of winning,
     * @param array $rate probability of winning, passed in as an array
     * @throws Exception
     * @author hgq <[email protected]>.
     * @date: 2018/05/07 10:05 AM
     */ 
    public  function setRate( $rate = array (1 )) {
         $this ->_rate = $rate ;
         if ( array_sum ( $this ->_rate) > 10000) // Check if there is a problem with the probability setting 
            throw  new  Exception (' Winning rate upto 100%' );
         if ( array_sum ( $this ->_rate) < 10000 )
             // Define the probability of not winning, if the probability given by the user is only 100, then ignore 0 
            $this ->_rate[] = 10000 - array_sum ( $this -> _rate);
    }

    /**
     * Randomly generate an integer seed of 1-10000 and submit it to the winning judgment function
     * @return int Sort by the incoming probability, return the number of winning items
     * @author hgq <[email protected]>.
     * @date: 2018/05/07 10:07 AM
     */
    public function runOnce() {
        return $this->judge(mt_rand(0, 10000));
    }

    /**
     * According to the set probability, determine whether an incoming random value wins the lottery
     * @param $seed random number within 10000
     * @return int $i is sorted by the incoming probability, and the number of winning items to return starts from 1. If there is only one prize, it is equal to 1, which is the winning prize.
     * @author hgq <[email protected]>.
     * @date: 2018/05/07 10:17 AM
     */
    protected function judge($seed) {
        foreach($this->_rate as $key => $value) {
            $tmpArr[$key + 1] = $value;
        }
        // accumulate the probabilities for random selection, combined into 
        $tmpArr [0] = 0 ;
         foreach ( $tmpArr  as  $key => $value ) {
             if ( $key > 0 ) {
                 $tmpArr [ $key ] += $ tmpArr [ $key - 1 ];
            }
        }
        for ( $i = 1; $i < count ( $tmpArr ); $i ++ ) {
             if ( $tmpArr [ $i - 1] < $seed && $seed <= $tmpArr [ $i ]) {
                 return  $ i ; // Return the number of winning items (in the order of probability settings) 
            }
        }
    }
}

Finally, take a look at the running effect and run it 10,000 times in a loop

The above set 6 products, the probabilities are 100, 200, 500, 600, 420, 512 respectively. The probability of winning the prize for multiple runs basically conforms to the probability setting. If the corresponding prize is set to 0, it means that the probability of winning the prize is 0.

Finally, why don't you get the grand prize?

In many similar lottery activities, participants often fail to draw big prizes. The author will give you an example from the perspective of the program. If I am the organizer of the lottery activity, I have set up 6 awards, and each award has a different winning prize. Probability, if the first prize is an iphoneX, but I set the probability of the prize to be 0, what does this mean?

This means that no matter how much you draw, you will never win the lottery.

Therefore, the lottery is risky, and the operation needs to be cautious~~

 

By  Hgq

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525400&siteId=291194637