Function for generating random numbers using PHP

Source: Internet
Author: User
Function for generating random numbers using PHP

  1. Function randomkeys ($ length)
  2. {
  3. $ Pattern = '1234567890abcdefghijklmnopqrstuvwxyz
  4. ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./& l
  5. T;> ?; #:@~ [] {}-_ = +) (* & ^ % ___ FCKpd ___ 0 pound ;"! '; // Character pool
  6. For ($ I = 0; $ I <$ length; $ I ++)
  7. {
  8. $ Key. = $ pattern {mt_rand ()}; // Generate a php random number
  9. }
  10. Return $ key;
  11. }
  12. Echo randomkeys (8); This php random function can generate XC * = z ~ A string like 7L is random enough! Now we will introduce another method to generate random numbers using PHP: Use the chr () function to save the steps to create a character pool. Function randomkeys ($ length)
  13. {
  14. $ Output = ";
  15. For ($ a = 0; $ a <$ length; $ a ++ ){
  16. $ Output. = chr (mt_rand (33,126); // Generate a php random number
  17. }
  18. Return $ output;
  19. }
  20. Echo randomkeys (8 );
  21. ?>

Note: In the second php random function, use mt_rand () to generate a php random number between 33 and 126, and then convert it to a character using the chr () function. View the ascii code table and you will find that 33 to 126 represents all the characters in the character pool of the first function. The second function has the same functionality as the first function, and is more concise.

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.