【问题标题】:Generate Unique Random String With Letters And Numbers In Lower Case生成带有小写字母和数字的唯一随机字符串
【发布时间】:2011-08-23 10:34:09
【问题描述】:

如何修复此代码,使其生成唯一的小写随机字母和数字?

api_string = (0...32).map{65.+(rand(25)).chr}.join    

目前,它只生成字母。

【问题讨论】:

标签: ruby ruby-on-rails-3


【解决方案1】:

较新版本的 Ruby 支持 SecureRandom.base58,这将获得比十六进制更密集的标记,没有任何特殊字符。

> SecureRandom.base58(24)
> "Zp9N4aYvQfz3E6CmEzkadoa2" 

【讨论】:

  • 这个答案比公认的要好得多。
【解决方案2】:

这将生成一个 32 到 50 个字符的较低随机字符串,包括数字和字母:

require 'string_pattern'

puts "32-50:/xN/".gen

【讨论】:

    【解决方案3】:
    Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond).to_s(36)
    

    【讨论】:

      【解决方案4】:

      8.times.map { [*'0'..'9', *'a'..'z'].sample }.join

      【讨论】:

      • '0'..'9' 生成一个从 0 到 9 的数组,'a'..'z' 生成一个从 a 到 z 的数组,但是它们之前的 * 究竟是做什么的呢?
      • '0'..'9' 是范围,* 是 splat 运算符。它将范围内的元素拆分为作为一个组返回的单个项目,因此基本上* 正在帮助您分离元素,以便可以使用sample 方法对它们进行采样
      【解决方案5】:

      使用 ruby​​ 语言的 SecureRandom。

      require 'securerandom' randomstring = SecureRandom.hex(5)
      

      会生成n*2个随机字符串,包含“0-9”和“a-f”

      【讨论】:

        【解决方案6】:

        我忘了从哪里来的,但我今天早上不知怎么读到了这篇文章

        l,m = 24,36
        rand(m**l).to_s(m).rjust(l,'0')
        

        它创建从 0 到 power(36,24) 的随机数,然后将其转换为 base-36 字符串(即 0-9 和 a-z)

        【讨论】:

        【解决方案7】:
        ((('a'..'z').to_a + (0..9).to_a)*3).shuffle[0,(rand(100).to_i)].join
        

        rand(100) 替换为rand(n),其中n 是所需字符串的最大长度。

        【讨论】:

          【解决方案8】:

          所有字母和数字,这就是数字以 36 为底的表示方式。

          api_string = Array.new(32){rand(36).to_s(36)}.join
          

          【讨论】:

            【解决方案9】:

            如果您使用的是 ruby​​ 1.9.2,则可以使用 SecureRandom:

            irb(main):001:0> require 'securerandom'
            => true
            irb(main):002:0> SecureRandom.hex(13)
            => "5bbf194bcf8740ae8c9ce49e97"
            irb(main):003:0> SecureRandom.hex(15)
            => "d2413503a9618bacfdb1745eafdb0f"
            irb(main):004:0> SecureRandom.hex(32)
            => "432e0a359bbf3669e6da610d57ea5d0cd9e2fceb93e7f7989305d89e31073690"
            

            【讨论】:

            • 这对于简单的需求来说很方便,但 alpha 范围只是 a-f,因此生成的字符串的安全性/复杂性并没有达到应有的水平。
            • 最好使用base64,同样内置:SecureRandom.base64(16).gsub(/=+$/,'')
            • API 密钥不是问题,但也有方便的 urlsafe_base64 方法:SecureRandom.urlsafe_base64(16) => "XpMnLfc3FySd-C4V2Ipxag"
            • 为什么使用base64更好
            • 更好SecureRandom.urlsafe_base64(length)
            【解决方案10】:

            这是一种方法:

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

            如果您有可用的 Active Support,您也可以这样做来制作类似 API 密钥的字符串:

            ActiveSupport::SecureRandom.hex(32)
            

            【讨论】:

              【解决方案11】:
              CHARS = (?0..?9).to_a + (?a..?z).to_a
              api_string = 32.times.inject("") {|s, i| s << CHARS[rand(CHARS.size)]}
              

              【讨论】:

                猜你喜欢
                • 2011-08-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-02-17
                • 1970-01-01
                • 1970-01-01
                • 2010-09-08
                • 2012-11-21
                相关资源
                最近更新 更多