递归函数:递归函数是指直接或间接的调用函数自身的函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        /**    
          * 获取几个指定范围不重复的数字
          *  按小到大排序
          * @param $digit
          * @return mixed
          */
         private function getHappyEight($digit$max){
              $number array();
              for ($i=1; $i<=$digit$i++){
                   $number[] = $this->getLotteryNum(1, $max$number);
              }
              //$number = array_unique($number);
              sort($number);
              return implode(','$number);
         }
          
        /**
          * 使用递归 获取不重复数字
          * @param $min
          * @param $max
          * @param $number
          * @return mixed
          */
         private function getLotteryNum($min$max$number){
             $num =  sprintf("%02d",mt_rand($min$max));
             if(in_array($num$number)) {
                  $num $this->getLotteryNum($min$max$number);
             }
             return $num;
         }
          
        //获取100以内 10个不重复的数字 
        $this->getHappyEight(10,100);