4 ms·
A bit more efficient: 14.times.map { rand(33..126).chr }.shuffle.join
by Axsuul 7y ago
A bit more efficient:
14.times.map { rand(33..126).chr }.shuffle.join
- oh_sigh 7y agoAnd 3x more possibilities because it allows the same character more than once. But I think the last shuffle is not necessary
- cgranier 7y agoThanks! Learned something new today (even though I’m mostly on Python now)
- e12e 7y agoWhy shuffle? Does it do something rand doesn't already do? At any rate, would probably want to use https://ruby-doc.org/stdlib-2.5.1/libdoc/securerandom/rdoc/Securerandom.html https://ruby-doc.org/stdlib-2.5.1/libdoc/securerandom/rdoc/S... for password generation. Also, I think you might just want an alphabet and use sample: https://ruby-doc.org/core-2.5.1/Array.html#method-i-sample https://ruby-doc.org/core-2.5.1/Array.html#method-i-sample # unsure if you need a: require 'securerandom' (33..126).sample(14, random: SecureRandom).map(&:chr).join
- Axsuul 7y agoYou're right, probably doesn't need shuffle :)