Pseudo-Random number PHP random number generation code and use case analysis

Source: Internet
Author: User
Tags repetition
We can also use random numbers to design any program structure that we imagine.
First, let's meet the random number function rand () provided by PHP. The rand () function of PHP returns a random integer, using the following method
Rand (Min,max)
The optional parameter min and Max allows rand () to return a pseudo-random integer between 0 and Rand_max. For example, want a random number between 5 and 15 (including 5 and 15), with Rand (5, 15).
Let me take a look at a specific example, we do a basic function call, do not set the specific parameters, we get the random number will not be limited by the Min and max two parameters.

Copy the Code code as follows:




Results: 652696728 (random result) 1, using PHP to generate random numbers within a specified interval if we are going to generate a random number between two numbers, we need to set two parameters for Rand: So we get the results in our control, he should be Minnum <= Result <= Maxnum; Suppose we are going to use PHP to generate random numbers between 10000 and 2000, our code should write this:

Copy the Code code as follows:


Echo (rand (1000,2000));? >


Simple enough, here's a little bit more difficult. At the beginning of this article we said that the random number is very useful and we can solve some complicated problems with PHP random number. 2, using PHP to get a set of random elements will be set we need to get a random element from an array

Copy the Code code as follows:


$my _array=array (' ASP ', ' PHP ', ' JAVASCRIPT ', ' AJAX ', ' CSS ', ' JQUERY ', ' HTML ');
Echo ($my _array[rand (0,6));
?>


As you can imagine, the result might be any element that is 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 to between 0 and 6. Below we use two sets of random numbers to enhance the above example, we need a random number for conditional judgment, and another random number to do the output of the element.

Copy the Code code 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 results we get may look like this:


First run we get three results

Since we use a random number to limit the number of bars displayed, the resulting results, in addition to the article random, show the number of bars is also random, such as:


Second run gets seven results

Perhaps you would ask, PHP random number can only do these boring things? Rand () doesn't seem so important; you're wrong, think about it. Random numbers are playing an important role in these applications, including verification codes, random article extraction of some CMS systems, and download address assignment. In addition, in the area of security and algorithms, Many applications of random numbers are also worthy of further study, such as encryption and the same congruential structure.

The above describes the pseudo-random number of PHP random number generation code and use case analysis, including the pseudo-random number of content, I hope the PHP tutorial interested in a friend helpful.

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