From: nobu@... Date: 2015-02-13T02:41:30+00:00 Subject: [ruby-core:68104] [Ruby trunk - Feature #10849] Adding an alphanumeric function to SecureRandom Issue #10849 has been updated by Nobuyoshi Nakada. > The length of the result string is about 4/3 of _n_. I don't like this interface. ~~~ruby def self.choose(source, length) size = source.size length.times.map {source[random_number(size)]}.join('') end GRAPH = [*'!'..'~'] def self.graph(length) choose(GRAPH, length) end ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9'] # SecureRandom.alphanumeric generates a random alphanumeric string. # # The argument _n_ specifies the length of the random number to be # generated. # # The result may contain A-Z, a-z and 0-9. # # p SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbRccA" # p SecureRandom.alphanumeric #=> "6BbW0pxO0YENxn38HMUbcQ" # # If a secure random number generator is not available, # +NotImplementedError+ is raised. def self.alphanumeric(length) choose(ALPHANUMERIC, length) end ~~~ ---------------------------------------- Feature #10849: Adding an alphanumeric function to SecureRandom https://bugs.ruby-lang.org/issues/10849#change-51482 * Author: Andrew Butterfield * Status: Open * Priority: Normal * Assignee: Andrew Butterfield ---------------------------------------- It would be handy to have a method that produces a random alphanumeric string. ---Files-------------------------------- securerandom.txt (1.32 KB) securerandom.patch (1.32 KB) -- https://bugs.ruby-lang.org/