• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php彩票类lottery

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php

 /*
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

 /**
 * Lottery class
 *
 * @author      Rafal Strojek <[email protected]>
 * @copyright   2014 (c) Rafal Strojek
 * @version     0.1
 */

class Lottery
{
    /**
     * Default parameters
     */
    private $params = array();
    
    /**
     * Numbers to drawn
     */
    private $numbers = array();
    
    /**
     * Constructor
     *
     * @param   array  $params  User-defined parameters
     */
    public function __construct($params = array())
    {
        $this->params = array_merge($this->getDefaultParameters(),$params);
        $this->numbers = range($this->params['from'], $this->params['to'], 1);
        
        $this->seedRand();
    }
    
    /**
     * Gets default parameters
     *
     * @return  array  Default Parameters
     */
    public function getDefaultParameters()
    {
        return array(
            'from' => 1, 
            'to' => 49,
            'numbers' => 6,
            'seed' => (int) ((float) microtime() * 1000000),
            'pow' => pow(2,24),
        );
    }
    
    /**
     * Gets parameters
     *
     * @return  array  Lottery parameters
     */
    public function getParameters()
    {
        return $this->params;
    }
    
    public function getSeed()
    {
        return (int) $this->params['seed'];
    }
    
    private function setSeed($seed = null)
    {
        $this->params['seed'] = ($seed) ? $seed : $this->makeSeed();

        // Return instance to shortcut
        return $this;
    }
    
    private function seedRand()
    {
        mt_srand($this->getSeed());
    }
    
    private function makeSeed()
    {
        return (int) ((mt_rand() + ((float) microtime() * 1000000)) % $this->params['pow']);
    }
    
    public function createTicket()
    {
        $ticket = array();
        $array = $this->numbers;
        
        for($i = 0; $i < $this->params['numbers']; $i++)
        {
            $this->setSeed()->seedRand();
            $key = mt_rand(0, (count($array) - 1));
            
            $ticket[$i] = $array[$key];
            array_splice($array, $key, 1);
        }
        
        asort($ticket);
        
        return $ticket;
    }
    
    public function createTickets($count = null)
    {
        if($count <= 0)
        {
            return array();
        }
        
        $count = min(500, max(1, $count));
        $tickets = array();
        for($i = 0; $i < $count; $i++)
        {
            $tickets[$i] = $this->createTicket();
        }
        
        return $tickets;
        
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP二次开发discuz3.2最新体验发布时间:2022-07-10
下一篇:
PHPMySQLi增删改查发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap