Search code examples
algorithmhashchecksum

Universal algorithm for generating properities


I need to make algorithm, what take random unique string (hash, whatever) and generate multiple random properities from prepaired lists.

Example: [cat algorithm]

color: white,black,brown
eye-color: blue, green, brown
size: 10,20,30

function algorithm(random_unique_hash){
   ...magic...
   return [color, eye-color, size]
}

Returned cat is absolutely unique to her string. String is unique in whole list of strings. Every cat must be different. I need to be able to make hash again from properities.

Is this possible? Or it needs to work in different way? Did I need to make cat and then make unique checksum?

Where to start?


Solution

  • You've got three colors, three eye-colors, and three sizes. That means there are exactly 3*3*3=27 different cats you can make. So the random_unique_hash should just be a number from 0 to 26.

    To get the properties from the hash, break the hash down into indexes using division and the modulo operator, e.g.

    index1 = random_unique_hash % 3
    index2 = (random_unique_hash / 3) % 3
    index3 = random_unique_hash / 9
    

    The indexes can then be used to access the correct entry in each property list.

    To get the hash from the properties you'll need to find the index of the property. Then combine all of the indexes to recreate the hash, like this

    random_unique_hash = index3 * 9 + index2 * 3 + index1