Home  >  Article  >  Backend Development  >  How to make array random and non-repeating in php

How to make array random and non-repeating in php

PHPz
PHPzOriginal
2020-09-25 15:51:153487browse

php method to implement random and non-repeating arrays: first create a PHP sample file; then use the "array_flip()" function to flip the keys and values ​​of the array; then use the PHP array feature to cover repetitions key and flip it again; finally remove the duplicate values.

How to make array random and non-repeating in php

Recommendation: "PHP Video Tutorial"

Write several types of generation below without duplication For the random number method, just go to the code

<?php
define(&#39;RANDOM_MAX&#39;, 100);
define(&#39;COUNT&#39;, 10);
 
echo &#39;max random num: &#39;.RANDOM_MAX, &#39; ;result count:&#39;.COUNT, &#39;<br/>&#39;;
 
invoke_entry(&#39;rand1&#39;);
invoke_entry(&#39;rand2&#39;);
invoke_entry(&#39;rand3&#39;);
invoke_entry(&#39;rand4&#39;);
 
function invoke_entry($func_name) {
    $time = new time();
    $time->time_start();
    call_user_func($func_name);
    echo $func_name.&#39; time spend: &#39;, $time->time_spend();
    echo &#39;<br/>&#39;;
}
function rand1() {
    $numbers = range (1, RANDOM_MAX);
    shuffle($numbers);  //随机打乱数组
    $result = array_slice($numbers, 1, COUNT);
    return $result;
}
function rand2() {
    $result = array();
    while(count($result)< COUNT) {
        $result[] = mt_rand(1, RANDOM_MAX); //mt_rand()是比rand()更好更快的随机函数
        $result = array_unique($result); //删除数组中重复的元素
    }
    return $result;
}
function rand3() {
    $result = array();  
    while(count($result) < COUNT) {
        $_tmp = mt_rand(1, RANDOM_MAX);
        if(!in_array($_tmp, $result)) { //当数组中不存在相同的元素时,才允许插入
            $result[] = $_tmp;
        }
    }  
    return $result;
}
function rand4() {
    $result = array();
    while (count($result) < COUNT) {
        $result[] = mt_rand(1, RANDOM_MAX);
        $result = array_flip(array_flip($result));  //array_flip将数组的key和value交换
    }
    return $result;
}
class time {
    private $_start;
     
    public function time_start() {
        $this->_start = $this->microtime_float();
    }
    public function time_spend() {
        return $this->microtime_float() - $this->_start;
    }
    private function microtime_float() {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
}
 
 
?>

Let’s talk about the fourth method, which is the flip method. Use array_flip() to flip the keys and values ​​of the array, and use the php array Feature, duplicate keys will be overwritten. At this time, flipping again is the same as removing duplicate values.

The above methods are just simple examples, and some methods have limited scope of application.

Let’s look at the efficiency of several methods:



The performance of using array_unique() is poor when the array is large. Of course, shuffle() will also be affected by this.

The above is the detailed content of How to make array random and non-repeating in php. For more information, please follow other related articles on the PHP Chinese website!

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