Created
July 24, 2008 18:57
Getting a unique random item in a collection
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
private string GetRandomRelatedViewID(string viewIDList) | |
{ | |
BaseGenericList<BrandEnsemble.Framework.RelatedItems.Components.RelatedItem> riList = BrandEnsemble.Framework.RelatedItems.RelatedItems.GetAllRelatedItems(); | |
riList.Filter(new string[] { "Caption" }, string.Empty, FilterOperand.NotEquals); | |
BrandEnsemble.Framework.RelatedItems.Components.RelatedItem rri = riList.RandomItem(); | |
while (viewIDList.IndexOf(rri.ViewID) >= 0) | |
{ | |
rri = riList.RandomItem(); | |
} | |
return rri.ViewID; | |
} | |
// this function goes into the BaseGenericCollection | |
public T RandomItem() | |
{ | |
int randomRI = 0; | |
Random random = new Random(); | |
lock (random) | |
{ | |
randomRI = random.Next(0, this.Count - 1); | |
} | |
return this[randomRI]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment