Home > Article > Backend Development > rand() function for php random number generation
I believe everyone is familiar with the rand() function. Many friends have come to this function during the development process. They all know that this function generates PHP random numbers, and the rand() function returns PHP random numbers.INTEGER! Today I will take you to learn more about the rand() function of random number generation in PHP!
First, let’s get to know the random number function rand() provided by php. The rand() function of php will return a random integer. The specific usage is as follows:
rand(min,max)
The optional parameters min and max can be Make rand() 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).
Below I Let's look at a specific example. 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. The code is as follows:
The result obtained :652696728 (Random result) 1. Use PHP to generate random numbers within the specified interval. If we want to generate random numbers between two numbers, we need to set two parameters for rand: In this way, the result we get is in our Under 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:
echo(rand(1000,2000));?>
It’s simple enough, Let’s do something a little more difficult. At the beginning of this article, we said that random numbers play a great role. We can use PHP random numbers to solve some complex problems. 2. Using PHP to obtain random elements in a set will set what we need To get a random element from an array, the code is as follows:
$my_array=array('asp','php','网页','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 , our my_array array contains seven elements, we set the parameters of rand() between 0 and 6, below we use two sets of random numbers to enhance the function of the above example, we need a The random number is used for conditional judgment, and another random number is used as the output of the element. 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 51cto.com'); }
The result we get may be like the following:
First run We got three results. Since we used a random number to limit the number of displayed results, in addition to the random articles, the results we got were also random.
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 and some cms that can be seen everywhere Systematic random article extraction, download address allocation, etc. Random numbers play an important role in these applications. In addition, in the field of security and algorithms, many applications of random numbers are also worthy of our in-depth study, such as encryption and congruence structures. The code is as follows:
$ip2id= round(rand(600000, 2550000) / 10000); //第一种方法,直接生成 $ip3id= round(rand(600000, 2550000) / 10000); $ip4id= round(rand(600000, 2550000) / 10000); //下面是第二种方法,在以下数据中随机抽取 $arr_1 = array("218","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); $ip1id = $arr_1[$randarr]; echo $ip1id; echo "."; echo $ip2id; echo "."; echo $ip3id; echo "."; echo $ip4id; ?>
Summary:
This article is to introduce to you the rand() function for generating random numbers in PHP. For many friends It is a very good choice for us, I hope it will be helpful to your work~
Related recommendations:
The above is the detailed content of rand() function for php random number generation. For more information, please follow other related articles on the PHP Chinese website!