Skip to content

Instantly share code, notes, and snippets.

@smook1980
Created December 19, 2014 04:58
Show Gist options
  • Select an option

  • Save smook1980/768f18d8af557550e881 to your computer and use it in GitHub Desktop.

Select an option

Save smook1980/768f18d8af557550e881 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'securerandom'
CURRENT_NO_DB_RECORDS = 6_146_992
FACTOR = 10
ITERATIONS = CURRENT_NO_DB_RECORDS * FACTOR
def compute_hash_frequency
hash_counts = { }
ITERATIONS.times do
# The resulting size is around 4/3 the number of bytes given.
hash = SecureRandom.urlsafe_base64(6)
hash_counts[hash] = hash_counts.fetch(hash, 0) + 1
end
hash_counts
end
def check_it(hash_occurences)
puts "Calculating Collisions..."
actual_unique_hash_count = hash_occurences.keys.count
puts "We expected #{ITERATIONS} results and got #{actual_unique_hash_count} unique hashes back."
puts "Difference?: #{ITERATIONS - actual_unique_hash_count}"
puts "Passes?: #{ITERATIONS == actual_unique_hash_count}\n\n"
min, max = hash_occurences.keys.map { |k| k.length }.minmax
puts "The min length of hash was #{min}, the max #{max}"
end
check_it compute_hash_frequency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment