using System;
using System.Collections.Generic;
public static class RandomElementSelector
{
public static IList<T> CollectAllowedElements<T>(IList<T> allElements, IList<T> excludedElements)
{
List<T> allowedElements = new List<T>();
foreach (T element in allElements)
if (!excludedElements.Contains(element))
allowedElements.Add(element);
return allowedElements;
}
public static T SelectRandomElement<T>(IList<T> allowedElements)
{
Random random = new Random();
int randomIndex = random.Next(allowedElements.Count);
return allowedElements[randomIndex];
}
public static T SelectRandomElement<T>(IList<T> allElements, IList<T> excludedElements)
{
IList<T> allowedElements = CollectAllowedElements(allElements, excludedElements);
return SelectRandomElement(allowedElements);
}
}
public class Test
{
public static void Main()
{
const int N = 100;
// Example #1
int[] allNumbers = new int[N];
for (int i = 0; i < allNumbers.Length; ++i)
allNumbers[i] = i + 1;
int[] excludedNumbers = { 5, 7, 17, 23 };
Console.WriteLine(RandomElementSelector.SelectRandomElement(allNumbers, excludedNumbers));
// Example #2
List<string> allStrings = new List<string>();
for (int i = 0; i < N; ++i)
allStrings.Add("Item #" + (i + 1));
string[] excludedStrings = { "Item #5", "Item #7", "Item #17", "Item #23" };
Console.WriteLine(RandomElementSelector.SelectRandomElement(allStrings, excludedStrings));
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKIApwdWJsaWMgc3RhdGljIGNsYXNzIFJhbmRvbUVsZW1lbnRTZWxlY3Rvcgp7CiAgICBwdWJsaWMgc3RhdGljIElMaXN0PFQ+IENvbGxlY3RBbGxvd2VkRWxlbWVudHM8VD4oSUxpc3Q8VD4gYWxsRWxlbWVudHMsIElMaXN0PFQ+IGV4Y2x1ZGVkRWxlbWVudHMpCiAgICB7CiAgICAgICAgTGlzdDxUPiBhbGxvd2VkRWxlbWVudHMgPSBuZXcgTGlzdDxUPigpOwogICAgICAgIGZvcmVhY2ggKFQgZWxlbWVudCBpbiBhbGxFbGVtZW50cykKICAgICAgICAgICAgaWYgKCFleGNsdWRlZEVsZW1lbnRzLkNvbnRhaW5zKGVsZW1lbnQpKQogICAgICAgICAgICAgICAgYWxsb3dlZEVsZW1lbnRzLkFkZChlbGVtZW50KTsKICAgICAgICByZXR1cm4gYWxsb3dlZEVsZW1lbnRzOwogICAgfQogICAgCiAgICBwdWJsaWMgc3RhdGljIFQgU2VsZWN0UmFuZG9tRWxlbWVudDxUPihJTGlzdDxUPiBhbGxvd2VkRWxlbWVudHMpCiAgICB7CiAgICAgICAgUmFuZG9tIHJhbmRvbSA9IG5ldyBSYW5kb20oKTsKICAgICAgICBpbnQgcmFuZG9tSW5kZXggPSByYW5kb20uTmV4dChhbGxvd2VkRWxlbWVudHMuQ291bnQpOwogICAgICAgIHJldHVybiBhbGxvd2VkRWxlbWVudHNbcmFuZG9tSW5kZXhdOwogICAgfQogCiAgICBwdWJsaWMgc3RhdGljIFQgU2VsZWN0UmFuZG9tRWxlbWVudDxUPihJTGlzdDxUPiBhbGxFbGVtZW50cywgSUxpc3Q8VD4gZXhjbHVkZWRFbGVtZW50cykKICAgIHsKICAgICAgICBJTGlzdDxUPiBhbGxvd2VkRWxlbWVudHMgPSBDb2xsZWN0QWxsb3dlZEVsZW1lbnRzKGFsbEVsZW1lbnRzLCBleGNsdWRlZEVsZW1lbnRzKTsKICAgICAgICByZXR1cm4gU2VsZWN0UmFuZG9tRWxlbWVudChhbGxvd2VkRWxlbWVudHMpOwogICAgfQp9CiAKcHVibGljIGNsYXNzIFRlc3QKewogICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIGNvbnN0IGludCBOID0gMTAwOwogICAgICAgIAogICAgICAgIC8vIEV4YW1wbGUgIzEKICAgICAgICBpbnRbXSBhbGxOdW1iZXJzID0gbmV3IGludFtOXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGFsbE51bWJlcnMuTGVuZ3RoOyArK2kpCiAgICAgICAgICAgIGFsbE51bWJlcnNbaV0gPSBpICsgMTsKICAgICAgICBpbnRbXSBleGNsdWRlZE51bWJlcnMg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