The rand () function generated by PHP random number

Source: Internet
Author: User
Tags repetition
I believe that you are not unfamiliar with rand () function, many small partners in the development process of this function, all know that this function is generated PHP random number, the rand () function is to return the PHP random number of integers! Let's take a look at the rand () function generated by PHP random numbers today!

First, to meet the random number function provided by PHP rand (), 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, the code is 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:

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

Simple enough, the following point slightly somewhat difficult, at the beginning of this article we said that the role of random numbers, we can use PHP random number to solve some complex problems, 2, using PHP to get a set of random elements will be set we need to get a random element from an array, the code is as follows:

$my _array=array (' asp ', ' php ', ' web ', ' Ajax ', ' css ', ' jquery ', ' html ');  Echo ($my _array[rand (0,6));

As you can imagine, the results we get could 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 rand () to a parameter between 0 and 6. Here we use two sets of random numbers to enhance the above example, we need a random number for conditional judgment, another random number to do the output of the element, 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 learning '. $my _array[rand (0,6)]);  Echo (' on 51cto.com ');  }

The results we get may look like this:

First run we get three results, because we use a random number to limit the number of bars displayed, so the resulting results in addition to the article random, the number of bars displayed is also random.

Second run gets seven results

Perhaps you would ask, PHP random number can only do these boring things? Rand () doesn't seem to be so important; you're wrong, think about it. Random numbers play an important role in these applications, in addition to the random article extraction and download address allocation of some CMS systems, and in the field of security and algorithms, Many applications of random numbers are also worthy of further study, such as encryption and the same congruential structure, the code is as follows:

$ip 2id= Round (rand (600000, 2550000)/10000); The first method, the direct generation of the  $ip 3id= round (rand (600000, 2550000)/10000);  $ip 4id= Round (rand (600000, 2550000)/10000);  Here is the second method, randomly extracting  $arr _1 = Array ("218", "218", "66", "66", "218", "218", "60", "60", "202", "204", "66", "66", "66" in the following data, "The", "" "," "", "222", "221", "the" "," "," "," "," "", "" "," 218 "," 218 "," "", "" "," "", "" "," "," "122", "211";  $randarr = Mt_rand (0,count ($arr _1)-1);  $ip 1id = $arr _1[$randarr];  echo $ip 1id;  echo ".";  echo $ip 2id;  echo ".";  echo $ip 3id;  echo ".";  echo $ip 4id;  ? >

Summarize:

This article is to introduce the PHP random number generated by the rand () function, for many small partners is a very good choice, hope to your work is helpful ~

Related recommendations:

A method of generating PHP random numbers from digital letters


Five ways to generate PHP random numbers without repetition


A method for generating random numbers in PHP

Related Article

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.