Created
December 11, 2017 21:46
-
-
Save TheBuzzSaw/6ef9e75aa67ccb2123e8cba7234c524c to your computer and use it in GitHub Desktop.
Secret Santa algorithm!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace SecretSanta | |
| { | |
| class Program | |
| { | |
| static void GenerateSecretSantas(Random random, params string[] people) | |
| { | |
| var assignments = new string[people.Length]; | |
| Array.Copy(people, assignments, people.Length); | |
| int last = people.Length - 1; | |
| for (int i = 0; i < last; ++i) | |
| { | |
| int j = random.Next(i + 1, last); | |
| var swap = assignments[i]; | |
| assignments[i] = assignments[j]; | |
| assignments[j] = swap; | |
| } | |
| for (int i = 0; i < people.Length; ++i) | |
| { | |
| Console.WriteLine(people[i] + " is assigned to " + assignments[i]); | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| try | |
| { | |
| var random = new Random(DateTime.Now.Ticks.GetHashCode()); | |
| for (int i = 0; i < 4; ++i) | |
| { | |
| Console.WriteLine("TEST " + (i + 1)); | |
| GenerateSecretSantas(random, "Alice", "Bob", "Charlie", "Daniel", "Esther", "Frank"); | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| while (e != null) | |
| { | |
| Console.WriteLine(e.GetType()); | |
| Console.WriteLine(e.Message); | |
| Console.WriteLine(e.StackTrace); | |
| Console.WriteLine(); | |
| e = e.InnerException; | |
| } | |
| } | |
| finally | |
| { | |
| Console.WriteLine("PRESS ENTER"); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment