Home  >  Article  >  Backend Development  >  Generate a php representation of non-repeating random numbers.

Generate a php representation of non-repeating random numbers.

巴扎黑
巴扎黑Original
2016-11-12 10:52:13906browse

Premise:

I saw a post today and saw that someone wanted to generate an array. There are 10 elements in this array, all of which are integers and non-repeating random numbers between 1-60.

Of course, this question is not difficult! Below is the answer from the forum reply.

Code:

Php code

<?php   
          function get_randoms($min,$max,$num){  
                 $count = 0;  
                 $res = array();  
                 while($count<$num){  
                        $res[] = mt_random($min,$max);  
                        $res = array_flip(array_flip($res));   
                        $count = count($res);  
                  }  
                  return $res;  
           }  
             
            $result = get_randoms(1,60,10);  
?>



Here he uses $count to get the number of result arrays, and he makes the number of result arrays less than the target number in the while loop. Through this, we get $count which is an array of target number.
During each cycle, through mt_random($min,$max), a value within the range is randomly obtained, stored in the array $res, and then through two key-value exchanges, that is, through the uniqueness of the array key , so that there is no duplication.

After I read the code of this logic, I couldn't help but think deeply. What would you do if you didn't know the array_filp function?
To put it bluntly, it uses the uniqueness of the key so that there will be no duplication of random numbers. So can we simplify this problem?

Code:

Php code

<?php   
         function get_randoms($min,$max,$num){  
                $count = 0;  
                $res = array();  
                while($count<$num){  
                       $key = mt_random($min,$max);  
                       $res[$key] = $key;  
                       $count = count($res);  
                 }  
                 return $res;  
          }  
            
           $result = get_randoms(1,60,10);  
 ?>



Since then, we have solved this problem completely through the uniqueness of the array key. array_flip will not be used at all.

Through the above, I make two points.

​ ​ 1: Because PHP provides us with a large number of built-in functions, their execution efficiency is very high. When we encounter a problem, we try our best to use it to solve the problem. This is convenient and fast. Therefore, we work and study in our daily work, accumulate as many PHP functions as possible, and understand them deeply.

2: Although PHP provides us with a large number of built-in functions and the execution efficiency is also very high, I would like to point out that although the efficiency is high, it still takes time to execute. When we encounter a problem, we should not do it blindly. We can first understand the essence of the solution so that we can implement it skillfully.


Statement:
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