Creating a random name generator. How to do it?

I am trying to grab one item from each of the lists here and combine them to create a unique name. It's just for the bumps. :)

Here is the list:

List<string> FirstNames = new List<string>() { "Sergio", "Daniel", "Carolina", "David", "Reina", "Saul", "Bernard", "Danny", "Dimas", "Yuri", "Ivan", "Laura" }; List<string> LastNamesA = new List<string>() { "Tapia", "Gutierrez", "Rueda", "Galviz", "Yuli", "Rivera", "Mamami", "Saucedo", "Dominguez", "Escobar", "Martin", "Crespo" }; List<string> LastNamesB = new List<string>() { "Johnson", "Williams", "Jones", "Brown", "David", "Miller", "Wilson", "Anderson", "Thomas", "Jackson", "White", "Robinson" }; 

I know that I get one item through the index, and also know that I can use the Random class to generate a random number from 0 to ListFoo.Count.

What I don't know is to check if a random permutation from the collections has already been set out.

I was thinking about using the tuple class:

 List<Tuple<int,int,int>> permutations = new List<Tuple<int,int,int>>(); 

But I have a brainstorming here .;) Any directions? I'm really not looking for all the code for this simple problem, just a suggestion or a hint.


EDIT

Thanks to the suggestions given here, this is what I came up with. Any room for improvements?

 static void Main(string[] args) { List<string> FirstNames = new List<string>() { "Sergio", "Daniel", "Carolina", "David", "Reina", "Saul", "Bernard", "Danny", "Dimas", "Yuri", "Ivan", "Laura" }; List<string> LastNamesA = new List<string>() { "Tapia", "Gutierrez", "Rueda", "Galviz", "Yuli", "Rivera", "Mamami", "Saucedo", "Dominguez", "Escobar", "Martin", "Crespo" }; List<string> LastNamesB = new List<string>() { "Johnson", "Williams", "Jones", "Brown", "David", "Miller", "Wilson", "Anderson", "Thomas", "Jackson", "White", "Robinson" }; var permutations = new List<Tuple<int, int, int>>(); List<string> generatedNames = new List<string>(); Random random = new Random(); int a, b, c; //We want to generate 500 names. while (permutations.Count < 500) { a = random.Next(0, FirstNames.Count); b = random.Next(0, FirstNames.Count); c = random.Next(0, FirstNames.Count); Tuple<int, int, int> tuple = new Tuple<int, int, int>(a, b, c); if (!permutations.Contains(tuple)) { permutations.Add(tuple); } } foreach (var tuple in permutations) { generatedNames.Add(string.Format("{0} {1} {2}", FirstNames[tuple.Item1], LastNamesA[tuple.Item2], LastNamesB[tuple.Item3]) ); } foreach (var n in generatedNames) { Console.WriteLine(n); } Console.ReadKey(); } 
+4
source share
6 answers

You are on the right track! Each time you create a name, add it to your list of tuples

 //Create the tuple Tuple <int, int, int> tuple = new Tuple<int, int, int>(index1, index2, index3) if(!permutations.Contains(tuple)) { permutations.Add(tuple); //Do something else } 
+4
source

I would think that the simplest solution is to simply collect the collected name in a HashSet<string> , which will ensure the uniqueness of the list of created names.

+2
source

An alternative to HashSet's answer is to collect all possible combinations in advance, shuffle them, and then save them in a queue, where you can get them one at a time. This will avoid the need to check existing ones at each new creation and still be random.

This only works if you don't have a large dataset, since the work of creating a complete list and shuffling will be huge for a large dataset.

+1
source

It is very easy to generate them using LINQ:

 var combs = (from first in FirstNames from second in LastNamesA from third in LastNamesB select new Tuple<string, string, string>(first, second, third)).ToList(); 

After that, if you need to randomly select unique items from the list, simply shuffle the list, and then select them in turn.

You can use the Knut-Fisher-Yate algorithm (which moves in place):

 Random rand = new Random(); for (int i = combs.Count - 1; i > 0; i--) { int n = rand.Next(i + 1); var mem = combs[i]; combs[i] = combs[n]; combs[n] = mem; } 
+1
source
  • Create a new 3-digit tuple
  • Check if permutations contain new tuple
  • If not => Add a new tuple to the list. If so, start from step 1 again.
0
source

I would create a HashSet<int> and save the numeric representation of the selection (e.g. 135 for the first, third, and fifth or use 010305) and then check if they are in the set.

0
source

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


All Articles