Several methods of generating random codes

Method one:

1, create a random integer from 33 to 126, such as 35,

2. Convert 35 to the corresponding ASCII character, for example, 35 corresponds to #

3. Repeat steps 1 and 2 for n times to create a password with n digits

Mt_rand (int $min, int $Max) ($min, int $Max); For example, digits 97 to 122 in the ASCII code table correspond to letters A to Z. For details, see the ASCII code table. The CHR (int $ASCII) function is used to convert the corresponding integer $ASCII to the corresponding character.

view sourceprint?

  1. function create_password($pw_length = 8) 
  2.     $randpwd = ”; 
  3.     for ($i = 0; $i < $pw_length; $i++)  
  4.     { 
  5.         $randpwd .= chr(mt_rand(33, 126)); 
  6.     } 
  7.     return $randpwd; 
  8.  
  9. // Call this function, passing the length argument $pw_length = 6
  10. echo create_password(6); 

Method 2:

Preset a string $chars, including a — z, a — z, 0 — 9, and some special characters

Select a random character from the $chars string

3. Repeat step 2 n times to obtain a password of length N

view sourceprint?

  1. function generate_password( $length = 8 ) { 
  2. // Password character set, you can add any character you want
  3. $chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789! @ # $% ^ & * () – _ [] {} + =, < > ~ `.; : /? | ‘;
  4.  
  5.     $password = ”; 
  6.     for ( $i = 0; $i < $length; $i++ )  
  7.     { 
  8. // There are two ways to get characters
  9. // The first is to use substr to intercept any character in $chars;
  10. // The second is to take any element of the character array $chars
  11.         // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); 
  12.         $password .= $chars[ mt_rand(0, strlen($chars) – 1) ]; 
  13.     } 
  14.  
  15.     return $password; 

Method 3:

Preset an array of $chars, including a — z, a — z, 0 — 9, and some special characters

Select $length from array $chars by array_rand()

$chars; $chars; $chars; The disadvantage of this method is that the same character will not be fetched twice.

view sourceprint?

  1. function make_password( $length = 8 ) 
  2. // Password character set, you can add any character you want
  3.     $chars = array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’,  
  4.     ‘i’, ‘j’, ‘k’, ‘l’,’m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’,  
  5.     ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’,’z’, ‘A’, ‘B’, ‘C’, ‘D’,  
  6.     ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’,’M’, ‘N’, ‘O’,  
  7.     ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’,’Z’,  
  8. ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘! ‘,
  9. ‘@’, ‘#’, ‘$’,’ % ‘, ‘^’, ‘&’, ‘*’, ‘(‘,’) ‘, ‘-‘, ‘_’.
  10. ‘(‘,’) ‘, ‘{‘,’} ‘, ‘<‘ and ‘>’, ‘-‘, ‘`’, ‘+’ and ‘=’, ‘, ‘,
  11. ‘. ‘, ‘; ‘, ‘:’, ‘/’, ‘? ‘, ‘|’);
  12.  
  13. // Select a random $length element in $chars
  14.     $keys = array_rand($chars, $length);  
  15.  
  16.     $password = ”; 
  17.     for($i = 0; $i < $length; $i++) 
  18.     { 
  19. // concatenate $length into a string
  20.         $password .= $chars[$keys[$i]]; 
  21.     } 
  22.  
  23.     return $password; 

Method 4:

This method is a new method provided by a netizen after this article was reproduced by blue Ideal. The algorithm is simple and the code is short. Just because of the return value of md5() function, the generated password only includes letters and numbers. Algorithm idea:

1. Time () gets the current Unix timestamp

2. Encrypt the timestamp obtained in the first step with MD5 ()

3. Encrypt the result of the second step and intercept n bits to get the desired password

view sourceprint?

  1. function get_password( $length = 8 )  
  2.     $str = substr(md5(time()), 0, 6); 
  3.     return $str; 

Time efficiency comparison

Using the following PHP code, we calculate the running time of the above four random password generators to generate 6-digit passwords, and then do a simple comparison of their time efficiency.

view sourceprint?

  1. function getmicrotime() 
  2.     list($usec, $sec) = explode(” “,microtime()); 
  3.     return ((float)$usec + (float)$sec); 
  4.  
  5. // Record the start time
  6. $time_start = getmicrotime(); 
  7.  
  8. // Put the PHP code to execute here, such as:
  9. // echo create_password(6); 
  10.  
  11. // Record the end time
  12. $time_end = getmicrotime(); 
  13. $time = $time_end – $time_start; 
  14.  
  15. // Outputs the total running time
  16. Echo “execute time $time seconds”;
  17. ? >

The final result:

Method 1:9.8943710327148E-5 seconds

Method 2:9.6797943115234E-5 seconds

Method three: 0.00017499923706055 seconds

Method four: 3.4093856811523E-5 seconds

You can see that method one and method two have similar execution times, method four has the shortest running time, and method three has a slightly longer running time.