I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z":
value = ""; 8.times{value << (65 + rand(25)).chr}
but it doesn't look clean, and it can't be passed as an argument since it isn't a single statement. To get a mixed-case string "a" .. "z" plus "A" .. "Z", I changed it to:
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
but it looks like trash.
Does anyone have a better method?
My 2 cents:
I like Radar's answer best, so far, I think. I'd tweak a bit like this:
With this method you can pass in an abitrary length. It's set as a default as 6.
Array.new(n){[*"0".."9"].sample}.join
, where n=8 in your case.Generalized:
Array.new(n){[*"A".."Z", *"0".."9"].sample}.join
, etc. - from this answerOthers have mentioned something similar, but this uses the URL safe function.
The result may contain A-Z, a-z, 0-9, “-” and “_”. “=” is also used if padding is true.
Here is one simple code for random password with lenth 8
Hope it will help.