Home > php教程 > php手册 > 奇怪的洗牌逻辑

奇怪的洗牌逻辑

WBOY
Release: 2016-06-06 19:35:54
Original
854 people have browsed it

跑了1W次,洗牌逻辑耗时2.5019571781158,没有出现重复率,但是感觉奇奇怪怪的,先留着吧。回头再说 无 class demo{ private $dic = array('a', 'b', 'c', 'd'); // 花色 private $kings = array('w1', 'w2'); // 大小王 private $card_set = array(); // 扑克

跑了1W次,洗牌逻辑耗时2.5019571781158, 没有出现重复率,但是感觉奇奇怪怪的,先留着吧。回头再说
class demo
{
    private $dic = array('a', 'b', 'c', 'd'); // 花色

    private $kings = array('w1', 'w2'); // 大小王

    private $card_set = array(); // 扑克牌

    private $card_array = array(); // 洗好的牌


    // 执行随机洗牌
    public function getCardSet()
    {
        $this->createCard();

        for($i = 0; $i < 54; $i++)
        {
            $this->getRandNum();

            $k = $this->key;

            $this->card_array[$i] = $this->card_set[$k];

            unset($this->card_set[$k]);
        }

        ksort($this->card_array);

        return implode(',', $this->card_array);
    }

    // 生成扑克牌
    public function createCard()
    {
        $tmp_card = array();

        for($i = 1; $i <= 13; $i++)
        {
            $count = count($this->dic);

            for($j = 0; $j < $count; $j++)
            {
                $tmp_card[] = $this->dic[$j] . $i;
            }
        }

        $this->card_set = array_merge($tmp_card, $this->kings);
    }

    // 生成随机数
    public function getRandNum()
    {
        $num = microtime(true);
        $k = rand(0, 53);
        if(! isset($this->card_set[$k]))
        {
            $this->getRandNum();
        }
        else
        {
            $this->key = $k;
        }
    }
}
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template