Generate Password() : mt_rand « Math « PHP






Generate Password()

 
<?php

function GeneratePassword($min = 5, $max = 8) {
  $ValidChars = "abcdefghijklmnopqrstuvwxyz123456789";
  $max_char = strlen($ValidChars) - 1;
  $length = mt_rand($min, $max);
  $password = "";
  for ($i = 0; $i < $length; $i++) {
    $password .= $ValidChars[mt_rand(0, $max_char)];
  }
  return $password;
}

echo "New Password = " . GeneratePassword() . "\n";
echo "New Password = " . GeneratePassword(4, 10) . "\n";
?>
  
  








Related examples in the same category

1.int mt_rand ( [int min, int max] ) returns random numbers, similar to the rand( ).
2.Finding a random line of a file
3.Generate random floating-point values from 0 to 10 with two decimals
4.Generate random numbers
5.Get a random value from 5 to 25.
6.Get random values from –10 to 10.
7.Random floating-point values from 0 to 10 with two decimals
8.Random numbe with precision
9.Random values are not restricted to positive integers. The following example shows how to get random values from -10 to 10.