Created
June 27, 2014 17:08
-
-
Save tompave/3b9ed34878893c0b9fc0 to your computer and use it in GitHub Desktop.
SecureRandom effectiveness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
REGEX = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*[^\w])|(?=.*_))/ | |
def run | |
times = 0 | |
word = begin | |
times += 1 | |
SecureRandom.urlsafe_base64 | |
end until word =~ REGEX | |
times | |
end | |
attempts = (1..1_000_000).map do | |
run | |
end | |
counts = {} | |
attempts.each do |i| | |
counts[i] ||= 0 | |
counts[i] += 1 | |
end | |
=begin | |
counts: | |
{2=>249217, | |
1=>470640, | |
3=>132049, | |
4=>69634, | |
5=>36868, | |
9=>2948, | |
10=>1533, | |
6=>19600, | |
7=>10347, | |
8=>5420, | |
12=>443, | |
11=>808, | |
14=>117, | |
17=>26, | |
13=>238, | |
15=>54, | |
16=>40, | |
20=>5, | |
19=>3, | |
18=>8, | |
21=>2} | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment