jeudi 18 mars 2021

How to get rid of duplicates from random name generator?

I'm trying to make a random generator that selects a specific number of names from a list and pairs them together.

private void button1_Click(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines(@"C:\Users\Brown\source\repos\Generator\bin\names.txt");

            List<string> source = new List<string>();
            int n = int.Parse(textBox1.Text);
            for (int i = 0; i < n; i++)
            {
                source.Add(lines[new Random().Next(lines.Length)]);
            }
            string joinNames = string.Join(" x ", source);

            genResult.Text = joinNames;
        }

I got it to work but when i click the generate button I see duplicates of some names. I'm very new to C# so any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire