Random password

Generates a random password by using given characters.

function random_password($length, $characters='abcdefgh1234567890'){
    if ($characters == ''){ return ''; }
    $chars_length = strlen($characters)-1;
    $pwd = '';
    while(strlen($pwd) < $length){
        $rand_char = mt_rand(0, $chars_length);
        $pwd .= $characters[$rand_char];
    return $pwd;
Snippet Details

random_password(5,'abcd1234') => returns something like: '2b4d3'

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!

Older comments: