Kod opisałem, o to chodziło?
/// <summary>
/// Klasa do losowania dwóch liczb ze zbioru x,y niepowtarzających się
/// </summary>
public class MyRand
{
/// <summary>
/// Tworzy nowe wystąpienie instancji obiektu <see cref="MyRand"/>
/// </summary>
/// <param name="Array1">Liczby zbioru 1. Ilość elementów musi być identyczna jak w zbiorze <see cref="Array2"/></param>
/// <param name="Array2">Liczby zbioru 2. Ilość elementów musi być identyczna jak w zbiorze <see cref="Array1"/></param>
/// <exception cref="ArgumentException"/>
public MyRand(IEnumerable<int> Array1, IEnumerable<int> Array2)
{
if (Array1 == null || Array1.Count() == 0 || Array2 == null || Array2.Count() == 0)
throw new ArgumentException("Przynajmniej jeden ze zbiorów jest pusty lub ilość elementów w zbiorach nie jest identyczna");
array1 = Array1.ToList();
array2 = Array2.ToList();
}
/// <summary>
/// Pozostałe liczby w zbiorze nr 1;
/// </summary>
private List<int> array1;
/// <summary>
/// Pozostałe liczby w zbiorze nr 2
/// </summary>
private List<int> array2;
/// <summary>
/// Losuje liczbę ze zbioru <see cref="MyRand.array1"/> (Key) oraz ze zbioru <see cref="MyRand.array2"/> (Value)
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"
public KeyValuePair<int, int> GetNext()
{
if (array1.Count() == 0 || array2.Count() == 0)
throw new InvalidOperationException("Skończyły się liczby w zbiorach");
Random random1 = new Random();
int index1 = random1.Next(0, array1.Count());
int index2 = random1.Next(0, array2.Count());
KeyValuePair<int, int> ret = new KeyValuePair<int, int>(array1[index1], array2[index2]);
return ret;
}
/// <summary>
/// Losuje liczbę ze zbioru <see cref="MyRand.array1"/> (Key) oraz ze zbioru <see cref="MyRand.array2"/> (Value). Na koniec usuwa wylosowane liczby ze zbioru
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"
public KeyValuePair<int, int> GetNextAndDelete()
{
if (array1.Count() == 0 || array2.Count() == 0)
throw new InvalidOperationException("Skończyły się liczby w zbiorach");
Random random1 = new Random();
int index1 = random1.Next(0, array1.Count());
int index2 = random1.Next(0, array2.Count());
KeyValuePair<int, int> ret = new KeyValuePair<int, int>(array1[index1], array2[index2]);
array1.RemoveAt(index1);
array2.RemoveAt(index2);
return ret;
}
/// <summary>
/// Losuje i usuwa wszystkie elementy ze zbiorów
/// </summary>
public IEnumerable<KeyValuePair<int, int>> RandAllElements
{
get
{
while(array1.Count > 0)
{
yield return GetNextAndDelete();
}
}
}
}
Przykład użycia:
MyRand myRand = new MyRand(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
private void Button_Click(object sender, RoutedEventArgs e)
{
var v = myRand.GetNextAndDelete();
hList.Items.Add("Zbiór 1: " + v.Key + ", Zbiór 2: " + v.Value);
}
Przykład użycia 2:
MyRand myRand = new MyRand(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
private void Button_Click(object sender, RoutedEventArgs e)
{
foreach(var v in myRand.RandAllElements)
hList.Items.Add("Zbiór 1: " + v.Key + ", Zbiór 2: " + v.Value);
}