PHP Mt_rand () random number function

Source: Internet
Author: User

Mt_rand () returns a random integer using the Mersenne twister algorithm.

Syntax: Mt_rand (Min,max)

Note: If you do not provide optional parameters min and Max,mt_rand () return the pseudo-random number between 0 and Rand_max, such as a random number between 5 and 15 (including 5 and 15), use Mt_rand (5,15).
In the previous version of 3.0.7, Max meant a range, and to get the same number of random numbers in these versions as the previous example is 5 to 15, the short example is Mt_rand (5, 11).
The PHP instance code is as follows:

$rand = Mt_rand (0,1);

if ($rand ==0)

{

$array = Array (41,20,26,29,30);

}

ElseIf ($rand ==1)

{

$array = Array (38,42,37,400,444);

}//Open source software: phpfensi.com

foreach ($array as $v => $vv)

{

echo "$VVN";

}

Using time and Mt_rand functions to get random names

<?php
/**
* Created by Phpstorm.
* User:administrator
* DATE:2016/9/19 0019
* time:19:47
*/
function Rdname () {
$t = getdate ();
$year = $t [' Year '];
$month = $t [' Mon ']<10? "0" $t [' mon ']: $t [' mon '];
$day = $t [' Mday ']>9] $t [' Mday ']: "0" $t [' mday '];
$hour = $t [' Hours ']>9] $t [' Hours ']: "0" $t [' hours '];
$min = $t [' minutes ']>9] $t [' minutes ']: "0" $t [' minutes '];
$sec = $t [' seconds ']>9] $t [' seconds ']: "0" $t [' seconds '];
$randnum =mt_rand (1000,9999);//Get a random 4 digits
$randname = $year. $month. $day. $hour $min. $sec.//The date of the year and the random number splicing together
return $randname//returns a new name
}
$newname =rdname ()//Assign the returned name to $newname
echo $newname;?>

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.