PHP rand () random function generation with number code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array asp basic code copy echo example function

First, get to know the random number function rand () provided by the PHP tutorial. The rand () function of PHP returns a random integer, using the following method

Rand (Min,max)

Optional parameters min and Max can cause 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 specific parameters, we get the random number will not be the Min and max two parameters limit.
Copy code code as follows:

? Echo (rand ()); > Results: 652696728 (Random results) 1, using PHP to generate the random number in the specified interval if we want to generate a random number between two digits, we need to set two parameters for Rand: So we get the result right in our control, he should be minnum <= result <= Maxnum; Suppose we want to use PHP to generate random numbers between 10000 and 2000, and our code should write this:
Copy code code as follows:
Echo (rand (1000,2000)); >

Easy enough, here's a little bit more difficult. At the beginning of this article, we said that the role of random number is very large, we can use PHP random number to solve some complex problems. 2. Using PHP to get a random element in a set we need to get a random element from an array
Copy code code as follows:

$my _array=array (' asp tutorials ', ' php ', ' web effects ', ' Ajax ', ' CSS tutorials ', ' jquery ', ' html ');
Echo ($my _array[rand (0,6)));
?> can imagine that the results we get might be any of the elements contained in an array, such as ASP, PHP, or JavaScript. Note that our My_array array contains seven elements, and we set the parameters of rand () to between 0 and 6. Here we use two sets of random numbers to enhance the above example, we need a random number to judge the condition, and another random number to do the output of the element.
Copy 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 might 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 are random in addition to the article, and the number of bars displayed is stochastic, as shown below:


The second run gets seven results

You may ask, PHP random number can only do these boring things? Rand () doesn't seem to be that important; you're wrong, think about it everywhere. Verification code, some CMS system random article extraction, download address assignment, and so on, random number in these applications play an important role; In addition, in the area of security and algorithms, Many applications of random numbers are also worthy of further study, such as encryption and congruential structure

<?php
$ip 2id= round (rand (600000, 2550000)/10000)///first method, directly generate
$ip 3id= round (rand (600000, 2550000)/ 10000);
$ip 4id= Round (rand (600000, 2550000)/10000);
//Below is the second method that randomly extracts
$arr _1 = Array ("218", in the following data). 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);
$ip 1id = $arr _1[$randarr];
Echo $ip 1id;
Echo.;
Echo $ip 2id;
Echo.;
Echo $ip 3id;
Echo.;
Echo $ip 4id;

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.