Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created July 24, 2008 18:57
Show Gist options
  • Save dieseltravis/2247 to your computer and use it in GitHub Desktop.
Save dieseltravis/2247 to your computer and use it in GitHub Desktop.
Getting a unique random item in a collection
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