We can also use random numbers to design any program structure we imagine.
First, let's take a look at the random number function rand () provided by PHP (). The rand () function of PHP returns a random integer. The usage is as follows:
Rand (min, max)
The optional min and max parameters enable rand () to return a pseudo-random integer between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (including 5 and 15), use rand (5, 15 ).
Let's take a look at a specific example. If we call a basic function without setting specific parameters, the random number we get will not be limited by the min and max parameters.
Copy codeThe Code is as follows:
<? Echo (rand ();?>
Result: 652696728 (random result) 1. Use PHP to generate a random number within a specified range. If we want to generate a random number between two numbers, we need to set two parameters for rand, the result we get is in our control. It should be MinNum <= result <= MaxNum; suppose we want to use PHP to generate a random number between 10000 and 2000, our code should be written as follows:
Copy codeThe Code is as follows:
Echo (rand ();?>
It's easy enough. Here is a little more difficult. At the beginning of this article, we have said that random numbers are very useful. We can use PHP random numbers to solve some complicated problems. 2. Using PHP to obtain a random element in a set will make it necessary to obtain a random element from an array.
Copy codeThe Code is as follows:
$ My_array = array ('asp ', 'php', 'javascript', 'ajax ', 'css', 'jquery ', 'html ');
Echo ($ my_array [rand (0, 6)]);
?>
As you can imagine, the result may be any element contained in an array, such as ASP, PHP, or JavaScript. Note that our my_array array contains seven elements. We set the rand () parameter to 0 to 6. In the following example, we use two sets of random numbers to enhance the function of the above example. We need a random number for conditional judgment, and another random number for element output.
Copy codeThe 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 learn'. $ my_array [rand (0, 6)]);
Echo ('on bkjia.com ');
}
?>
The result may be as follows:
498) this. width = 498; "border = 0>
We get three results for the first running.
Because we use a random number to limit the number of items displayed, the number of items displayed in addition to the random articles is also random, for example:
498) this. width = 498; "border = 0>
Seven results are displayed after the second operation.
Maybe you will ask, can PHP random numbers only do these boring things? Rand () doesn't seem so important. If you're wrong, think about the ubiquitous verification code, the extraction and allocation of random articles in some CMS systems. Random Numbers play an important role in these applications; in addition, in the security and algorithm fields, many applications of random numbers are also worth further research, such as encryption and the same-remainder method structure.