PHP random number generation code and instance analysis

Source: Internet
Author: User
Tags repetition
In PHP programming, the generation of random numbers is particularly important. We need to use a random number to display random records (such as video, user records, and article IDs) on our page ). 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.
The code is as follows:


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:
The 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.
The 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.
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 Learn'. $ my_array [rand (0, 6)]);
Echo ('On 51CTO.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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.