1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private static Random _randomCache;
/// <summary>
/// return a random item from source list
/// </summary>
/// <typeparam name="T">generic type</typeparam>
/// <param name="source">source list</param>
/// <returns>random item</returns>
public static T Random<T>(this IEnumerable<T> source)
{
// use static randomizer
if (_randomCache == null) _randomCache = new Random(DateTime.Now.Millisecond);
// get index of random item
int index = _randomCache.Next(source.Count());
// return first item after a skip of "index"
return source.Skip(index).First();
}
|