Generate Unique Random String With Letters And Numbers In Lower Case

前端 未结 11 1947
感情败类
感情败类 2020-12-23 09:45

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    
相关标签:
11条回答
  • 2020-12-23 10:29
    Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond).to_s(36)
    
    0 讨论(0)
  • 2020-12-23 10:30
    ((('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.

    0 讨论(0)
  • 2020-12-23 10:36

    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)

    0 讨论(0)
  • 2020-12-23 10:40

    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
    
    0 讨论(0)
  • 2020-12-23 10:41

    Newer versions of Ruby support SecureRandom.base58, which will get you much denser tokens than hex, without any special characters.

    > SecureRandom.base58(24)
    > "Zp9N4aYvQfz3E6CmEzkadoa2" 
    
    0 讨论(0)
提交回复
热议问题