Over a million developers have joined DZone.

Generate A Random Password

·
I recently had need to generate random passwords for users.  Obviously they need to be alphanumerica.  Here's what I came up with.


def newpass( len )
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    return newpass
end


It generates a random password using the upper and lower case alphabet, and the numbers 0 to 9, to the size passed in.

No effort is made to make an easy to read password.  For example, 0 (zero) _will_ show up next to O (capital o).  Yay.
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}