• php *类 lottery


    <?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 <strojek.rafal@gmail.com>
     * @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;
            
        }
    }
  • 相关阅读:
    .linearDrag on rigidbody / rigidbody2D in code?
    Unity5权威讲解+项目源码+MP4
    C#的扩展方法解说
    use crunch compression
    IIS服务命令
    使用批处理打开控制面板中的功能
    一次性在一个文件夹里建立多个文件夹
    bat 批处理切换到当前脚本所在文件夹
    %date~0,4%和 %time~0,2%等用法详解(转)
    DOS批处理高级教程(还不错)(转)
  • 原文地址:https://www.cnblogs.com/walter371/p/4099625.html
Copyright © 2020-2023  润新知