Tuesday, 22 August 2017

How to generate a random string in Ruby

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?

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...