Home  >  Article  >  Backend Development  >  Multiple random numbers within a specified range in php_PHP tutorial

Multiple random numbers within a specified range in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:431169browse

To generate random data in php, we can use rand and mt_rand to generate random data within a specified range. Let me introduce the method to you.

Calling the mt_rand() method can generate random numbers. The parameters are the minimum and maximum values ​​of the range. The function will return a random number between the minimum and maximum values.

Generating truly random numbers is not an easy task for calculations.

There are two methods in PHP that can generate random numbers. One classic function is called rand(), and the other more excellent function is mt_rand().


Example 1

$rand = mt_rand(1, 100);

echo $rand;

?> Example 2
The code is as follows
 代码如下 复制代码


$random =rand(0,1000);

或者

$rand = mt_rand(1, 100);

echo $rand;
?>

Copy code

 代码如下 复制代码

srand((double)microtime()*1000000);
$random =rand(0,1000);

$random =rand(0,1000);

 代码如下 复制代码

/**
*获取一定范围内的多个随机数字
*/
function yang_numberRand($begin = 0, $end = 20, $limit = 5){
    $rand_array = range($begin, $end);
    shuffle($rand_array); //调用现成的数组随机排列函数
    return array_slice($rand_array, 0, $limit); //截取前$limit个
}

or
The code is as follows Copy code
srand((double)microtime()*1000000); $random =rand(0,1000);
Example 3
The code is as follows Copy code
/** *Get multiple random numbers within a certain range */ function yang_numberRand($begin = 0, $end = 20, $limit = 5){ $rand_array = range($begin, $end); Shuffle($rand_array); //Call the ready-made array random arrangement function Return array_slice($rand_array, 0, $limit); //Intercept the first $limit pieces } http://www.bkjia.com/PHPjc/633065.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633065.htmlTechArticleTo generate random data in php, we can use rand and mt_rand to generate random data within a specified range. The following is Let me introduce the method to all my classmates. Call the mt_rand() method...
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