How can I fix this code so it generates unique random letters and numbers in lower case?
api_string = (0...32).map{65.+(rand(25)).chr}.join
Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond).to_s(36)
((('a'..'z').to_a + (0..9).to_a)*3).shuffle[0,(rand(100).to_i)].join
Replace rand(100)
with rand(n)
where n
is the maximum length of your desired string.
i forgot from where, but i've read this somehow this morning
l,m = 24,36
rand(m**l).to_s(m).rjust(l,'0')
it create random number from 0 to power(36,24), then convert it to base-36 string (that is 0-9 and a-z)
This will generate a lower random string from 32 to 50 characters including numbers and letters, both:
require 'string_pattern'
puts "32-50:/xN/".gen
Newer versions of Ruby support SecureRandom.base58, which will get you much denser tokens than hex, without any special characters.
> SecureRandom.base58(24)
> "Zp9N4aYvQfz3E6CmEzkadoa2"