Skip to content

Instantly share code, notes, and snippets.

@Supernats
Created February 10, 2017 22:23
Show Gist options
  • Save Supernats/6af61521064c69ac9fab99356e27331c to your computer and use it in GitHub Desktop.
Save Supernats/6af61521064c69ac9fab99356e27331c to your computer and use it in GitHub Desktop.
require 'securerandom'
def secure_random_test(max = 1_000_000)
ratio = (0...max).map do
SecureRandom.random_number(max)
end.uniq.count.to_f/max.to_f
(ratio - (1 - 1/Math::E)).abs
end
puts "SecureRandom: #{secure_random_test}"
def rand_test(max = 1_000_000)
ratio = (0...max).map do
rand(max)
end.uniq.count.to_f/max.to_f
(ratio - (1 - 1/Math::E)).abs
end
puts "rand: #{rand_test}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment