views:

78

answers:

1

(Note: The project is in Python.)

I'm running a simulation in which I have many objects that I want to show on the screen and manipulate with. There needs to be a way to identify each object, because they'll be moving from place to place abruptly and I want to be able to track which object moved where.

What I've been thinking is, to every object I'll generate a "personality". A couple of colors, and an english name, and I'll put that as the object representation on the GUI. I figured that a hash function will be used to make these colors and names, but I've never worked with hash functions.

How can I do what I want to do?

+2  A: 

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

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

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

You can decompose the fields properly to create a color or a name (by mapping a bucket of names to a specific field). Of course you are limiting your hash (the real identity is always the uuid) but for visual purposes it's greatly sufficient. For example, you could use the first three octets to generate the color #9d477d, and the remaining octet c7 to pick one name out of a set of 256.

If you end up with too ugly colors, you can work in HSV instead, and clamp saturation and value to given levels. again, this restricts your hash even more (but the color space is already pretty limited after all).

Stefano Borini
Sounds good. Now, for the names, is there a ready list of english names I could use?
cool-RR
http://www.world-english.org/boys_names_list.htmPlease don't use the algorithm to give a name to your child ;)
Stefano Borini
It will be a bummer to scrape these from the website... is there anywhere an organized list?
cool-RR