Share 4 ways to generate non-repeating random numbers and arrays in PHP

coldplay.xixi
Release: 2023-04-09 10:56:01
forward
2888 people have browsed it

Share 4 ways to generate non-repeating random numbers and arrays in PHP

Write below a few methods of generating non-repeating random numbers. Let’s go directly to the code.

The code is as follows:

'; invoke_entry('rand1');invoke_entry('rand2');invoke_entry('rand3');invoke_entry('rand4'); function invoke_entry($func_name) { $time = new time(); $time->time_start(); call_user_func($func_name); echo $func_name.' time spend: ', $time->time_spend(); echo '
';}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); }} ?>
Copy after login

Let’s talk about the fourth method. , is the flip method. Usearray_flip()to flip the keys and values of the array. Using the PHP array feature, the duplicate keys will be overwritten. At this time, flipping again is the same as removing the duplicate values. .
The above methods are just simple examples, and some methods have limited scope of application.

Let’s take a look at the efficiency of several methods:

Usearray_unique()in Performance is poor when the array is large, and of courseshuffle()will also be affected by this.

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of Share 4 ways to generate non-repeating random numbers and arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!