PHP random number generation code and use case analysis

  • 2020-03-31 21:38:39
  • OfStack

We can also use random Numbers to design any program structure we want.

Let's start with the random number function rand () that PHP provides. The rand() function in PHP returns a random integer, as shown below

Rand (min and Max)

The optional parameters min and Max cause rand() to return pseudo-random integers 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 me take a look at a specific example. We make a basic function call without setting specific parameters, and the random number we get will not be limited by the parameters min and Max.
 
<? echo(rand());?> 

1. Using 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, the result we get is in our control, it should be MinNum < = the result < = MaxNum; Suppose we were to use PHP to generate random Numbers between 10000 and 2000. Our code would look like this:
 
echo(rand(1000,2000));?> 

That's easy enough. Let's do something a little bit harder. As we said at the beginning of this article, random Numbers play a big role, and we can use PHP random Numbers to solve some complex problems. Using PHP to get a random element in a collection will assume that we need to get a random element from an array
 
$my_array=array('ASP','PHP','JAVASCRIPT','AJAX','CSS','JQUERY','HTML'); 
echo($my_array[rand(0,6)]); 
?> 

As you can imagine, the result could be ASP, PHP, or JavaScript, any of the elements contained in the array. Note that our my_array array contains seven elements, and we set the arguments to rand () to be between 0 and 6. Next, we will use two sets of random Numbers to enhance the above example. We need one random number for conditional judgment and another random number for element output.
 
$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'); 
} 
?> 

Our results might look like the following:

(link: https://www.jb51.net/upload/201104/20110408172910677.jpg)
The first run we got three results

Since we use a random number to limit the number of bars displayed, the results obtained are not only random, but also random, as shown in the following figure:

(link: https://www.jb51.net/upload/201104/20110408172910819.jpg)
The second run yielded seven results

You might ask, can PHP random Numbers only do this kind of crap? Rand () doesn't seem to matter that much; You're wrong. Think of the ubiquitous captcha, the random article extraction of some CMS, the distribution of download addresses, etc. Random Numbers play an important role in these applications. In addition, in the field of security and algorithm, many applications of random Numbers are also worth further study, such as encryption and congruence structure.


Related articles: