PHP紅包分配算法

<?php
/**
 * User: phpmianshi.com 
 * Date: 2020/3/31
 * Time: 10:16
 */
 
class redPack
{
    /**
     * 測試紅包生成
     */
     public function test(){
        for($i=0;$i<5;$i++){
            $num[$i]['a']=$this->getBonus(5,4);
        }
        var_dump($num);
    }
 
    /**
     *生成紅包數組
     * @param $bonus_total 紅包總額
     * @param $bonus_count 紅包個數
     * @param $bonus_max 每個小紅包的最大額
     * @param $bonus_min 每個小紅包的最小額
     * @return 存放生成的每個小紅包的值的一維數組
     */
    function getBonus($total = 0, $count = 0)
    {
        $yushu = ($total - intval($total)); //如果金額爲小數則取出小數位
        $bonus_total = ($total - $yushu) * 100; //如果金額爲小數則去除小數小計算分配
        $bonus_count = $count;
        $result = array();
        if ($bonus_total / $bonus_count > 1) {
            if (($bonus_total - $bonus_total / 4) / ($bonus_count - 1) >= 1) {
                $bonus_max = $bonus_total / 4;
                if (($bonus_total / 4) == ($bonus_total / $bonus_count)) {
                    $bonus_max += 50;
                }
            } else {
                for ($j = 0; $j < $count; $j++) {
                    $result[$j] = ($bonus_total / $bonus_count) / 100;
                }
                $r = rand(0, $count - 1);
                $result[$r] = ($bonus_total - $bonus_count * 1 + 1) / 100;
                //如果還有負數產生就重新分配
                $attr = array();
                foreach ($result as $k => $v) {
                    $attr[$k]['money'] = $v;
                    $attr[$k]['yili'] = 0;
                }
                return $attr;
            }
        } else {
            for ($k = 0; $k < $count; $k++) {
                $result[$k] = $total / $count / 100;
            }
            //如果還有負數產生就重新分配
            $attr = array();
            foreach ($result as $k => $v) {
                $attr[$k]['money'] = $v;
                $attr[$k]['yili'] = 0;
            }
            return $attr;
        }
        $bonus_min = 1;
 
 
        $average = $bonus_total / $bonus_count;
        //$average = $bonus_total/ $bonus_count;
 
        $a = $average - $bonus_min;
        $b = $bonus_max - $bonus_min;
 
        //這樣的隨機數的概率實際改變了,產生大數的可能性要比產生小數的概率要小。
        //這樣就實現了大部分紅包的值在平均數附近。大紅包和小紅包比較少。
        $range1 = $this->sqr($average - $bonus_min);
        $range2 = $this->sqr($bonus_max - $average);
 
        for ($i = 0; $i < $bonus_count; $i++) {
            //因爲小紅包的數量通常是要比大紅包的數量要多的,因爲這裏的概率要調換過來。
            //當隨機數>平均值,則產生小紅包
            //當隨機數<平均值,則產生大紅包
            if (rand($bonus_min, $bonus_max) > $average) {
                // 在平均線上減錢
                $temp = $bonus_min + $this->xRandom($bonus_min, $average);
                $result[$i] = $temp;
                $bonus_total -= $temp;
            } else {
                // 在平均線上加錢
                $temp = $bonus_max - $this->xRandom($average, $bonus_max);
                $result[$i] = $temp;
                $bonus_total -= $temp;
            }
        }
 
        // 如果還有餘錢,則嘗試加到小紅包裏,如果加不進去,則嘗試下一個。
        while ($bonus_total > 0) {
            for ($i = 0; $i < $bonus_count; $i++) {
                if ($bonus_total > 0 && $result[$i] < $bonus_max) {
                    $result[$i]++;
                    $bonus_total--;
                }
            }
        }
 
        // 如果錢是負數了,還得從已生成的小紅包中抽取回來
        while ($bonus_total < 0) {
            for ($i = 0; $i < $bonus_count; $i++) {
                if ($bonus_total < 0 && $result[$i] > $bonus_min) {
                    $result[$i]--;
                    $bonus_total++;
                }
            }
        }
 
        //如果還有負數產生就重新分配
        $attr = array();
 
        //隨機一個小紅包加入金額小數位
        $rands = rand(0, ($bonus_count - 1));
        $result[$rands] += $yushu * 100;
 
        $nums = 0;
        //處理輸出
        foreach ($result as $k => $v) {
            if ($v < 1) {
                $this->getBonus();
                die;
            }
            $attr[$k]['money'] = $v / 100;
            $attr[$k]['yili'] = 0;
            $nums += $v;
        }
        //dump($nums);
        //dump($result);
        return $attr;
    }
 
    /**
     * 求一個數的平方
     * @param $n
     */
    function sqr($n)
    {
        return $n * $n;
    }
 
    /**
     * 生成min和max之間的隨機數,但是概率不是平均的,從min到max方向概率逐漸加大。
     * 先平方,然後產生一個平方值範圍內的隨機數,再開方,這樣就產生了一種“膨脹”再“收縮”的效果。
     */
    function xRandom($bonus_min, $bonus_max)
    {
        $sqr = intval($this->sqr($bonus_max - $bonus_min));
        $rand_num = rand(0, ($sqr - 1));
        return intval(sqrt($rand_num));
    }
}
 
//測試生成紅包的數組
$redPack=new redPack();
$redPack->test();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章