PHP rand () random function generates code with number of numbers

Source: Internet
Author: User
Tags repetition
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:

<? Echo (rand ());? >

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:

<?php

$ip 2id= Round (rand (600000, 2550000)/10000); The first method, the direct generation of

$ip 3id= Round (rand (600000, 2550000)/10000);

$ip 4id= Round (rand (600000, 2550000)/10000);

Here is the second method, which is randomly extracted from the following data

$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);

$ip 1id = $arr _1[$randarr];

echo $ip 1id;

echo ".";

echo $ip 2id;

echo ".";

echo $ip 3id;

echo ".";

echo $ip 4id;

?>

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