C# – Are there common methods for hashing an input file to a fixed set of values

chash

Let's say I'm trying to generate a monster for use in a roleplaying game from an arbitrary piece of input data. Think Barcode Battler or a more-recent iPod game whose name escapes me.

It seems to me like the most straightforward way to generate a monster would be to use a hash function on the input data (say, an MP3 file) and use that hash value to pick from some predetermined set of monsters, or use pieces of the hash value to generate statistics for a custom monster.

The question is, are there obvious methods for taking an arbitrary piece of input data and hashing it to one of a fixed set of values? The primary goal of hashing algorithms is, after all, to avoid collisions. Instead, I'm suggesting that we want to guarantee them – that, given a predetermined set of 100 monsters, we want any given MP3 file to map to one of them.

This question isn't bound to a particular language, but I'm working in C#, so that would be my preference for discussion. Thanks!

Best Answer

Hash the file using any hash function of your choice, convert the result into an integer, and take the result modulo 100.

monsterId = hashResult % 100;

Note that if you later decide to add a new monster and change the code to % 101, nearly all hashes will suddenly map to different monsters.

Related Topic