How to get a Random Item with a LINQ Query

By Peter Bromberg

It is very easy to get a random item from an items source. You could also write this up as an extension method, but it is so simple, it's probably not worth the trouble.

public static Quotation GetRandomQuote()
        {
           Random gen = new Random((int)DateTime.UtcNow.Ticks);

             return Quotes.Skip(gen.Next(0, Quotes.Count() - 1) - 1).Take(1).SingleOrDefault();

        }

How to get a Random Item with a LINQ Query  (1016 Views)