PHP random number generation code and usage example analysis_PHP tutorial

WBOY
Release: 2016-07-21 15:30:36
Original
1161 people have browsed it

We can also use random numbers to design any program structure we can imagine.

First, let’s get to know the random number function rand() provided by PHP. PHP's rand() function will return a random integer. The specific usage method is as follows

rand(min,max)

The optional parameters min and max can make rand() return between 0 and RAND_MAX of pseudo-random integers. For example, if you want a random number between 5 and 15 (inclusive), use rand(5, 15).

Let me look at a specific example below. We make a basic function call without setting specific parameters. The random number we get will not be limited by the min and max parameters.

Copy code The code is as follows:



Result obtained: 652696728 (random result) 1. Use PHP to generate a random number within a specified interval. If we want to generate a random number between two numbers, we need to set two parameters for rand: In this way, we The result is under our control, it should be MinNum <= result <= MaxNum; assuming we want to use PHP to generate a random number between 10000 and 2000, our code should be written like this:
Copy code The code is as follows:

echo(rand(1000,2000));?>

It's simple enough, here comes something a little more difficult. At the beginning of this article, we said that random numbers are very useful. We can use PHP random numbers to solve some complex problems. 2. Use PHP to obtain a random element in a collection. Suppose we need to obtain a random element from an array
Copy code The code is as follows:

$my_array=array('ASP','PHP','JAVASCRIPT','AJAX','CSS','JQUERY','HTML');
echo($my_array[rand(0 ,6)]);
?>

It is conceivable that the result we get may be any element contained in the array such as ASP, PHP or JavaScript. Note that our my_array array contains seven elements, and we set the argument to rand() to be between 0 and 6. Next, we use two sets of random numbers to enhance the function of the above example. We need one random number for conditional judgment and another random number for the output of the element.
Copy code The code is as follows:

$my_array=array('ASP','PHP','JAVASCRIPT',' AJAX','CSS','JQUERY','HTML');
$repetition=rand(0,6);
for($i=0;$i<=$repetition;$i++){
echo('I am learning ' . $my_array[rand(0,6)]);
echo(' on BKJIA.com');
}
?>

The result we get may look like this:

PHP随机数结果示例
The first time we run it we get three results

Since we use a random number to limit the number of displayed articles, the results obtained are not only random articles, but also the number of displayed articles, as shown below:

PHP随机数
The second run got seven results

Maybe you will ask, can PHP random numbers only do these boring things? rand() does not seem to be that important; you are wrong, think about the verification codes that can be seen everywhere, and the random article extraction of some CMS systems , download address allocation, etc. Random numbers play an important role in these applications; in addition, in the fields of security and algorithms, many applications of random numbers are also worthy of our in-depth study, such as encryption and congruence structures.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323199.htmlTechArticleWe can also use random numbers to design any program structure we imagine. First, let’s get to know the random number function rand() provided by PHP. PHP's rand() function will return a random integer, specifically use...
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!