PHP random number generation and use analysis
in PHP programming, generation of random numbers is particularly important. We need to use random numbers to display random records in our pages (such as pictures, user records, articles ID, etc.). We can also use random numbers to design any program structure we envision.
first, let's get to know the random number function rand () provided by PHP. The rand () function of PHP returns a random integer, and the specific use of the method is as follows:
[code]rand (min, max) [/code]
optional parameter min and Max can make rand () return a pseudorandom integer between 0 and RAND_MAX. For example, you want a random number between 5 and 15 (including 5 and 15), using rand (5, 15).
below, I look at a specific example, we do a basic function call, without setting specific parameters, the random number we get will not be restricted by the two parameters of min and max. Echo (rand ());?.gt;
results: 652696728 (random results)
[b]1, using PHP to generate a random number [/b]
in a specified interval, if we are to generate a random number between two numbers, we need to set two parameters for Rand:
we get the results in our control, He should be
[code]MinNum echo (rand (10002000));.Gt; [/code]
simple enough, here's a little more difficult. At the beginning of this paper, we have said that the role of random numbers is very large. We can use PHP random numbers to solve some complex problems.
[b]2, using PHP to get a random element in a set [/b]
we need to get a random element
[code]$my_array=array from an array ('ASP','PHP','JAVASCRIPT','AJAX','CSS'); 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, and we set the rand () parameter between 0 and 6.
below we use two sets of random numbers to do the enhancement of the above examples. We need a random number to determine the condition, and the other is the output of the element.
[code] ($my_array=array'ASP','PHP','JAVASCRIPT','AJAX','CSS','JQUERY','HTML'); $repetition=rand (0,6); Ttp://images.51cto.com/files/uploadimg/20091202/172240329.jpg:1gxe65m7][/url:1gxe65m7]
first run, we get three results,
because we use a random number to limit the number of bars displayed, so the results are removed. The article is random, the number of items displayed is also random, as follows:
[url=http://images.51cto.com/files/uploadimg/20091202/172408440.jpg:1gxe65m7][/url:1gxe65m7]
second runs get seven results
maybe you will Ask, is the PHP random number only to do these boring things? Rand () doesn't seem so important; you are wrong, think about the verifying code that can be seen everywhere, some random articles from CMS systems, the allocation of downloading addresses, etc. random numbers play an important role in these applications; in addition, a lot of random numbers should be in the security and algorithmic fields. It is also worth studying deeply, such as encryption and congruence method.