One often needs unique ID strings to tag processes, threads, files or anything else that might be created in quantity. Traditionally these are created based on PIDs or similar system values. But it is not easy to visually recognise these strings, which makes debugging more difficult than it need be. This recipe creates readable and pronounceable ID strings that are much easier to work with.
Python, 40 lines
I hate debugging systems by trying to track IDs that look like "kSbT73oQ". It's much easier on the brain to scan strings like "levokosi". These are also pronounceable, so one can communicate with other members on the team. "Hey dude, what's up with file sadofabi -- it appears to be locked."
I many cases one does not need billions of IDs, so restricting the strings to alternating consonants and vowels, and a reasonable length, gives us plenty of scope. The function GetFriendlyID() generates these of length 8, using the built-in Python pseudo-random generator. If this is not good enough, increase the length or use a more random module. But before you do so, realise that the algorithm here allows over 40 million unique strings.
Of course it is sometimes useful to have ID strings based on PIDs or whatever, so they can be tied back to the originating process. In those cases this recipe is not applicable. There are also cases where pronunciation is the main constraint. Here one could run into problems with these letter choices, since in some English accents most vowels sound the same. A word dictionary approach may be more appropriate in those cases, but first consider how likely it is that you will end up with IDs that sound similar.
The GetUniqueFriendlyID() function sketches out a typical implementation, where we want to ensure the generated IDs are, in fact, unique. (I have now changed this to use sets, for searches that scale better with large data sets.)
The module-level code does a simple test so we can inspect some resulting strings. Baby name generator, anyone?