Random name generator

Started by
3 comments, last by XTAL256 14 years, 10 months ago
Hi all, I am creating a random name generator, and its going well so far. However, I want to have unique names. The problem is, the more names I create, the higher amount of duplicates are being generated. Is there a simple way too keep iterating until you have a full array of unique names ? I wrote a function which checks for duplicate names, and when generating 8K names I have somewhere between 100-200 duplicates. If needed, ill post my code, but I don't think its needed since its more of a theory question.
-Please rate me if you find my replies useful.
Advertisement
Use a set structure like C++'s std::set class or Python's set object and add names to the set until it reaches the size you want.
why would that not create duplicates then? o.O
-Please rate me if you find my replies useful.
Quote:Original post by Dieseltjuh
why would that not create duplicates then? o.O


A set, by definition, doesn't contain duplicates. So you randomly generate the name, add it to the set, and repeat until the set reaches a particular size.

The duplicates are still created, but the set will contain only the unique names.
[TheUnbeliever]
Quote:Original post by SiCrane
Use a set structure like C++'s std::set class or Python's set object and add names to the set until it reaches the size you want.

You just have to make sure the size is less than the maximum number of unique values that can be generated by your RNG, otherwise it will never fill up :)
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement