Program to randomly generate a set of non-repeating numbers in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 16:56:46
Original
1011 people have browsed it

The following is an introduction to two program codes for randomly generating a set of non-repeating numbers in PHP. Friends who need to learn can refer to them.

The code is as follows
 代码如下 复制代码

/**
* PHP获取一组随机数字不重复
*/
$a = microtime();
function createRandID($m){
// 产生一个从1到$m的数组
$arr = range(1,$m);
// 打乱数组
shuffle ($arr);
// 取前十个
for($i=0;$i<=10;$i++){
// 赋值给新数组$n
$n[] = $arr[$i];
}
// 返回这组数字
return implode($n,',');
}

echo createRandID(700000);
echo '
';
echo $a - microtime();
?>

Copy code

/**

* PHP gets a set of random numbers without repetition

​*/

$a = microtime();

function createRandID($m){

// Generate an array from 1 to $m

$arr = range(1,$m);

//Shuffle the array
代码如下 复制代码

/**
* PHP获取一组随机数字不重复
* 琼台博客
*/
$a = microtime();
function createRandID($m){
// 注意,要先声明一个空数组,否则while里的in_array会报错
$arr = array();
// 使用while循环,只要不够10个就永远循环
while(count($arr)<=10){
// 产生一个随机数
$a = rand(1,$m);
// 判断:如果产生的随机数不再数组里就赋值到数组里
// 主要避免产生重复的数字
if(!in_array($a,$arr)){
// 把随机数赋值到数组里
$arr[] = $a;
}
}
// 返回产生的随机数字
return implode($arr,',');
}
echo createRandID(700000);
echo '
';
echo $a - microtime();
?>

Shuffle ($arr);

// Take the first ten

for($i=0;$i<=10;$i++){

// Assign a value to the new array $ n
         $n[] = $arr[$i];                                                }

// Return this set of numbers

Return implode($n,',');

}


echo createRandID(700000);

echo '
';

echo $a - microtime();

?>

Execution result:

0.672761 As you can see from the above results, the time spent is 0.6. Let’s adjust the random number range from 700000 to 900000 and look at the execution results Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 7200000 bytes) in /data0/htdocs/www/a.php on line 10 The array is too big and the program cannot run! !
The code is as follows Copy code
/** * PHP gets a set of random numbers without repetition
* Qiongtai Blog
​* Qiongtai Blog <🎜> */<🎜> $a = microtime(); <🎜> function createRandID($m){ <🎜> // Note, you must first declare an empty array, otherwise in_array in while will report an error <🎜> $arr = array(); <🎜> // Use a while loop, as long as there are less than 10, it will loop forever <🎜> While(count($arr)<=10){ <🎜> // Generate a random number <🎜>           $a = rand(1,$m); <🎜> // Judgment: If the generated random number is no longer in the array, assign it to the array <🎜>                       // Mainly to avoid duplicate numbers <🎜> If(!in_array($a,$arr)){ <🎜> //Assign random numbers to the array <🎜>                $arr[] = $a;                                                               } <🎜> } <🎜> // Return the generated random number <🎜> Return implode($arr,','); <🎜> } <🎜> echo createRandID(700000); <🎜> echo '
'; echo $a - microtime(); ?> Execution result: 308326,155128,280424,493174,214855,219990,482837,66329,512934,232527,386975 0.00015699999999996 As can be seen from the above execution results, the time can be ignored at all. Let’s adjust the random number range from 700000 to 999999 and take a look at the execution results 392281,822956,401282,176255,143076,501802,393338,546922,21836,601991,362006 0.00013600000000002 The execution result has nothing to do with the maximum value setting, it still runs very fast! http://www.bkjia.com/PHPjc/631573.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631573.htmlTechArticleThe following is an introduction to two program codes for randomly generating a set of non-repeating numbers in PHP. Friends who need to learn Can be used for reference. The code is as follows Copy code ?php /** * PHP gets a set of random...
source:php.cn
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
Popular Tutorials
More>
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!