Go deep into PHP to get random numbers and letters

Source: Internet
Author: User
This article describes how to obtain random numbers and letters in PHP. For more information, see

This article describes how to obtain random numbers and letters in PHP. For more information, see

Method 1

The Code is as follows:


$ FileID = date ("Ymd-His"). '-'. rand (100,999 );
// $ FileID is a random number such as 20100903-132121-908
?>


Method 2

The Code is as follows:


Function randomkeys ($ length ){
$ ReturnStr = '';
$ Pattern = '1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklomnopqrstuvwxyz ';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ ReturnStr. = $ pattern {mt_rand (0, 61)}; // generate a php Random Number
}
Return $ returnStr;
}
Echo randomkeys (4 );
?>


Method 3

The Code is as follows:


// Seed user-defined functions use microseconds as the seed
Function seed ()
{
List ($ msec, $ sec) = explode ('', microtime ());
Return (float) $ sec;
}
// Broadcast the seed of the random number generator and use the srand function to call the returned result of the seed function.
Srand (seed ());
// Random number generated by the output. The random number ranges from 10 to 100.
Echo rand (10,100 );
?>


Is the above and below no different? They all randomly output numbers between 10-. It's too easy for new people to learn.

The Code is as follows:


Echo rand (10,100 );
?>
Mt_rand (10,100 );


Srand is the seed. If not set, the default value is 1.
Rand is a fixed operation of Parameters Using seeds.
You can give it a try. You don't need a seed or set a fixed seed to run rand.
Then turn off the browser, and then run rand.
You will find that the results are always the same
Let's talk about the rand () function, rand ([int min], [int max])
This function obtains a random number between min and max. If the maximum and minimum ranges of random numbers are not specified and the server space is not specified, this function automatically retrieves a random number from 0 to RAND_MAX.
However, if you only use the rand () function, the random number of the virtual host in Hong Kong is messy. It is best to use the srand () function every time before getting the random number to configure the new random number seed.
Explain the following usage of functions (these two functions are generally used in this way ):
Srand (double) microtime () * 1000000 );
$ Rand_number = rand ();
Microtime () returns two values: the current millisecond and timestamp. to extract a random number, we can only get one random number from the millisecond, (double) microtime () only the current millisecond value is returned.
Microtime () is the number of milliseconds in seconds. Therefore, the values are decimal places and multiplied by 1000000 to convert them into integers.

Their workflow is as follows:
(1): first, provide a "Seed" to srand (), which is a value of the unsigned_int type.
(2): _ then, the Hong Kong Space calls rand (). It returns a random number (in the range of _ 0 ~) based on the value provided to srand ~ Between 32767)
(3): Call rand () multiple times as needed to continuously obtain new random numbers.
(4): Whenever srand () can be provided with a new "Seed" to further "randomize" rand ()
Output result.

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.