-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinqExtensions.cs
38 lines (36 loc) · 1.54 KB
/
LinqExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Text;
namespace Abyssal.Common
{
/// <summary>
/// A set of LINQ extensions.
/// </summary>
public static class LinqExtensions
{
/// <summary>
/// Selects a random element from the provided array.
/// </summary>
/// <typeparam name="T">The type of the members of the provided array.</typeparam>
/// <param name="source">The array to select the element from.</param>
/// <param name="random">The random number generator engine to use. It will default to a new instance of <see cref="System.Random"/>.</param>
/// <returns>The selected element.</returns>
public static T Random<T>(this T[] source, Random? random = null)
{
random ??= new Random();
return source[random.Next(0, source.Length)];
}
/// <summary>
/// Selects a random element from the provided list.
/// </summary>
/// <typeparam name="T">The type of the members of the provided list.</typeparam>
/// <param name="source">The list to select the element from.</param>
/// <param name="random">The random number generator engine to use. It will default to a new instance of <see cref="System.Random"/>.</param>
/// <returns>The selected element.</returns>
public static T Random<T>(this IList<T> source, Random? random = null)
{
random ??= new Random();
return source[random.Next(0, source.Count)];
}
}
}