This generates passwords consisting of letters (lower and uppercase),
numbers
and printable punctuation (?,.-_[(, etc.) (ASCII 48 - 122).
$required_length = 10;
for ($i = 0; $i < $required_length; $i++) {
$passwd .= chr(int(rand(123 - 48) + 48));
}
print $passwd, "\n";
Discussion:
rand(123 - 48) + 48:
Generate a random (fractional) number > 0 and < 123 - 48. Than add 48.
This generates effectivly a number between 0 and 122.
int(...) means: throw away the fractional part of that number.
chr() turns the given number to the corresponding ASCII-Character.
Now you have a random character. This gets appended to the desired
string.
Lathe, rinse, repeat.
hope this helps,
cr
On Sat, 05 May 2001 12:02:15 , cherukuwada subrahmanyam said:
> Hi,
> Is there any easy way to generate random string of particluar length.
> The idea is to generate passwords randomly.
> I did it using rand function i.e. generating random number and replacing the
> number with corresponding alphabet.
> Is there any easy way???
> thanks,
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
>
>