diff options
-rw-r--r-- | lib/securerandom.rb | 26 | ||||
-rw-r--r-- | test/test_securerandom.rb | 7 |
2 files changed, 29 insertions, 4 deletions
diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 3ee5cbd4d6..97911d4d6a 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -243,7 +243,31 @@ module Random::Formatter # +NotImplementedError+ is raised. private def choose(source, n) size = source.size - n.times.map {source[random_number(size)]}.join('') + m = 1 + limit = size + while limit * size <= 0x100000000 + limit *= size + m += 1 + end + result = ''.dup + while m <= n + rs = random_number(limit) + is = rs.digits(size) + (m-is.length).times { is << 0 } + result << source.values_at(*is).join('') + n -= m + end + if 0 < n + rs = random_number(limit) + is = rs.digits(size) + if is.length < n + (n-is.length).times { is << 0 } + else + is.pop while n < is.length + end + result.concat source.values_at(*is).join('') + end + result end ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9'] diff --git a/test/test_securerandom.rb b/test/test_securerandom.rb index 9b2c8a5e67..5f08182115 100644 --- a/test/test_securerandom.rb +++ b/test/test_securerandom.rb @@ -144,9 +144,10 @@ end end def test_alphanumeric - 65.times do |idx| - an = @it.alphanumeric - assert_match(/^[0-9a-zA-Z]+$/, an) + 65.times do |n| + an = @it.alphanumeric(n) + assert_match(/^[0-9a-zA-Z]*$/, an) + assert_equal(n, an.length) end end |