Using a hash function to create an unforgettable personality for objects

(Note: the project is in Python.)

I run a simulation in which I have many objects that I want to display on screen and manipulate them. There must be a way to identify each object, because they will move from place to place suddenly, and I want to keep track of which object is moving there.

What I was thinking about, for each object I will create a "personality". Several colors and an English name, and I will put this as a representation of an object in a graphical interface. I realized that a hash function would be used to create these colors and names, but I never worked with hash functions.

How can I do what I want?

+3
source share
1 answer

use uuid (uuid module in python> = 2.5).

This uuid in version 4 is, by definition, random in all fields (except one)

>>> uuid.uuid4()
UUID('9d477dc7-a986-4e3d-aa4f-6e57f690be78')

You can decompose the fields correctly to create a color or name (by matching a bucket of names into a specific field). Of course, you limit your hash (the real identity is always uuid), but for visual purposes this is quite a lot. For example, you can use the first three octets to generate the color # 9d477d, and the remaining octet c7select one name from a set of 256.

If you end up with too ugly colors, you can work in HSV instead, as well as saturation and clip value for given levels. again, this further limits your hash (but the color space is already quite limited).

+2
source

Source: https://habr.com/ru/post/1723974/


All Articles