summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-21 16:12:46 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-21 16:12:46 +0000
commit6cba29b4220cc0277f209af9d172f4ec1ddb5f0e (patch)
tree4324a47627dba6650cc4797b9d26969c25f2092c /lib
parent2c27e52f8ebdf4d382a12b224707d14f2ca589ed (diff)
less random generations in Random::Formatter#choose.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60309 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/securerandom.rb26
1 files changed, 25 insertions, 1 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']