First, get to know the random number function rand () provided by the PHP tutorial. The rand () function of PHP returns a random integer, using the following method
Rand (Min,max)
Optional parameters min and Max can cause rand () to return a pseudo-random integer between 0 and Rand_max. For example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).
Let me take a look at a specific example, we do a basic function call, do not set the specific parameters, we get the random number will not be min and max two parameters of the limit.
Copy code code as follows:
?? Echo (rand ());? >
Results obtained: 652696728 (random results) 1, using PHP to generate the random number in the specified interval if we want to generate a random number between two digits, we need to set two parameters for Rand: In this way, we get the result in our control, he should be Minnum <= Result <= Maxnum If we want to use PHP to generate random numbers between 10000 and 2000, our code should write this:
Copy code code as follows:
Echo (rand (1000,2000));? >
Easy enough, here's a little bit more difficult. At the beginning of this article, we said that the role of random number is very large, we can use PHP random number to solve some complex problems. 2. Using PHP to get a random element in a set we need to get a random element from an array
Copy code code as follows:
$my _array=array (' asp tutorials ', ' php ', ' web effects ', ' Ajax ', ' CSS tutorials ', ' jquery ', ' html ');
Echo ($my _array[rand (0,6)]);
?>
As you can imagine, we get the result that any of the elements contained in an array can be ASP, PHP, or JavaScript. Note that our My_array array contains seven elements, and we set the parameters of rand () to between 0 and 6. Here we use two sets of random numbers to enhance the above example, we need a random number to judge the condition, and another random number to do the output of the element.
Copy code code 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 51cto.com ');
}
?>
The results we get might look like this:
First run, we get three results.
Since we use a random number to limit the number of bars displayed, the resulting results are random in addition to the article, and the number of bars displayed is stochastic, as shown below:
The second run gets seven results
Perhaps you will ask, PHP random number can only do these boring things? Rand () doesn't seem to be that important; you're wrong, think about it everywhere. Verification code, some CMS system random article extraction, download address assignment, and so on, random number in these applications play an important role; In addition, in the area of security and algorithms, Many applications of random numbers also deserve to be studied in depth, such as encryption and congruential structures.
<?php
$ip 2id= round (rand (600000, 2550000)/10000)//first method, direct generation
$ip 3id= round (Rand 600000, 2550000)/10000);
$ip 4id= Round (rand (600000, 2550000)/10000);
//Below is the second method that randomly extracts
$arr _1 = Array ("218", in the following data). 218 "," 66 "," 66 "," 218 "," 218 "," 60 "," 60 "," 202 "," 204 "," 66 "," 66 "," 66 "," 59 "," 61 "," 60 "," 222 "," 221 "," 66 "," 59 "," 60 "," 60 "," 66 "," 218 "," 218 "," 62 "," 63 "," 64 "," 66 "," 66 "," 122 "," 211 ");
$randarr = Mt_rand (0,count ($arr _1)-1);
$ip 1id = $arr _1[$randarr];
Echo $ip 1id;
echo ".";
Echo $ip 2id;
echo ".";
Echo $ip 3id;
echo ".";
Echo $ip 4id;
?