Reputation: 1274
What is the most efficient way to hash random strings (of maximum 1024 characters, they can be UTF8) to integer values from 0 to 100? I think calculating CRC is the fastest but I'm not sure.
EDIT: The only requirement for this hash is to be super fast and not to change between sessions (same data => same hash value)
Upvotes: 0
Views: 1802
Reputation: 80085
Try the following:
"af".sum % 100 # => 99
"ag".sum % 100 # => 0
Upvotes: 6
Reputation: 26528
Use any hash function, convert that to an integer value, then calculate the modulus of 100.
Example using MD5:
require 'digest/md5'
def hash_100(str)
Digest::MD5.hexdigest(str).to_i(16) % 100
end
hash_100('hello') # => 94
hash_100('world') # => 51
I suspect there's a way to get the integer straight from the Digest without going through a hex first, but this was on top of my head.
Upvotes: 3