Generate Unique Random String With Letters And Numbers In Lower Case

前端 未结 11 2081
感情败类
感情败类 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:26

    Here's one way to do it:

    POSSIBLE = (('A'..'Z').to_a + (0..9).to_a)
    api_string = (0...32).map { |n| POSSIBLE.sample }.join
    

    If you have Active Support available you can also do this to make an API-key-like string:

    ActiveSupport::SecureRandom.hex(32)
    

提交回复
热议问题