Phprand () random function generation code-PHP source code

Source: Internet
Author: User
Ec (2); first, let's take a look at the random number function rand () provided by the php tutorial (). The rand () function of php returns a random integer. The usage is as follows: rand (min, max) optional parameters min and max can be used to enable rand () returns 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. Let's make a basic function call without setting specific parameters, script ec (2); script

First, let's take a look at the random number function rand () provided by the php tutorial (). 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 example', 'php', 'webpage effect', 'ajax ', 'css example', '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:


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:


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 structure.

$ Ip2id = round (rand (600000,255 0000)/10000); // The first method is directly generated.
$ Ip3id = round (rand (600000,255 0000)/10000 );
$ Ip4id = round (rand (600000,255 0000)/10000 );
// The second method is as follows:
$ 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;
?>

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.