Robert Grezan
Robert Grezan

Reputation: 1274

Fast and efficient way to hash long string to values in a given range

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

Answers (2)

steenslag
steenslag

Reputation: 80085

Try the following:

"af".sum % 100 # => 99
"ag".sum % 100 # => 0

Upvotes: 6

Douglas F Shearer
Douglas F Shearer

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

Related Questions