}//END _Delete_Button_Click() /// <summary> /// Randomly pairs names /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void _Generate_Button_Click(object sender, RoutedEventArgs e) { try { bool pairsComplete = false; while (pairsComplete == false) { CryptoRandomNumberGen random = new CryptoRandomNumberGen(); //reset _ViewModel.NamePairs = new Dictionary <string, string>(); //These are the names remaining to be picked List <string> namesRemaining = new List <string>(_ViewModel.Names); namesRemaining.Shuffle(); //These are the people picking List <string> namesForPicking = new List <string>(_ViewModel.Names); namesForPicking.Shuffle(); //go through each name foreach (string name in namesForPicking) { //Only allow valid names for this person List <string> validNamesForPerson = new List <string>(namesRemaining); validNamesForPerson.Remove(name); //Need to ensure that we don't reach a case there the last two names are the same if (validNamesForPerson.Count > 0) { //chose a random person for this person string chosenName = validNamesForPerson[random.Next(0, validNamesForPerson.Count)]; _ViewModel.NamePairs[name] = chosenName; namesRemaining.Remove(chosenName); } else { //we failed break; } } //Did we get through all names? if (namesRemaining.Count == 0) { pairsComplete = true; } } Properties.Settings.Default.SavedNames = string.Join(",", _ViewModel.Names.ToList()); Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show($"ERROR OCCURED: {ex.Message}.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); } }//END _Generate_Button_Click()