2020年12月22日星期二

Assign to each item another item in C# [duplicate]

I want to write a Christmas lottery algorithm. I want the people I enter in my hand to be matched with other people who are not themselves and printed on the screen. I wrote a code like the one below. But because it removes an element from the list every time, my program matches fewer people than the number of people entered.

How can I find a solution to this problem?

List<string> person = new List<string>() { "Joe","John","Chris","Henry","Tom","Patrick"};          List<string> personCopy = new List<string>(person);            for (int i = 0; i < person.Count; i++)          {              string person1 = person[i];              Random rnd = new Random();              var index = rnd.Next(personCopy.Count);              while (i == index) {                  index = rnd.Next(personCopy.Count);              }                string person2 = personCopy[index];              person.RemoveAt(i);              personCopy.RemoveAt(index);              Console.WriteLine("{0}  > {1}",person1,person2);                        }  
https://stackoverflow.com/questions/65417326/assign-to-each-item-another-item-in-c-sharp December 23, 2020 at 07:48AM

没有评论:

发表评论