leegod leegod - 3 years ago 97
C# Question

How to select random elements from list exclude some list?

So I have CardBase abstract class and
I want to extract some random card elements exclude some specific card list.
So I did like this.

public List<CardBase> GetRandExclude(List<CardBase> list, int elementsCount, List<CardBase> excludeList)
{
var returnCards = from card in list
where !excludeList.Contains(card)
select card;
foreach (CardBase cd in returnCards.Take(elementsCount))
{
Debug.Log("Selected random card is "+cd.name);
}
return (List<CardBase>) returnCards.Take(elementsCount);
}


Did I do right?
Are there better way?
Thanks in advance.




So I changed a little like this. And this seems works.

public List<CardBase> GetRandExclude(List<CardBase> list, int elementsCount, List<CardBase> excludeList)
{
var returnCards = from card in list
where !excludeList.Contains(card)
select card;
foreach (CardBase cd in returnCards.OrderBy(arg => Guid.NewGuid()).Take(elementsCount).ToList())
{
Debug.Log("Selected random card is "+cd.name);
}
return returnCards.OrderBy(arg => Guid.NewGuid()).Take(elementsCount).ToList();
}

Answer Source

You could go the Linq route and use Except provided CardBase has some form of equality comparison. Other wise you could use the overload and provide a IEqualityComparer<CardBase> comparer

From there you want to randomly select cards from the available cards remaining.

static Random randomizer = new Random();
public List<CardBase> GetRandExclude(List<CardBase> list, int elementsCount, List<CardBase> excludeList) {
    var availableCards = list.Except(excludeList).ToList();
    int count = Math.Min(elementsCount, availableCards.Count);
    var selectedCards = new HashSet<CardBase>();
    do{
        var index = randomizer.Next(0, availableCards.Count);
        var card = availableCards[index];
        selectedCards.Add(card);
    } while (selectedCards.Count < count);
    foreach (CardBase cd in selectedCards) {
        Debug.Log("Selected random card is " + cd.name);
    }
    return selectedCards.ToList();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download