1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

🚚 Rename Random.OneOf to Random.NextFrom

Item for #21
This commit is contained in:
Oliver Booth 2021-01-18 15:19:06 +00:00
parent dd66fb9a2b
commit 1b80267238

View File

@ -9,11 +9,7 @@ namespace X10D
/// </summary> /// </summary>
public static class RandomExtensions public static class RandomExtensions
{ {
/// <summary> internal static readonly Random Random = new();
/// Gets the <see cref="System.Random" /> instance to which other extension methods may refer, when one is
/// needed but not provided.
/// </summary>
internal static Random Random { get; } = new Random();
/// <summary> /// <summary>
/// Returns either <see langword="true" /> or <see langword="false" /> based on the next generation of the current /// Returns either <see langword="true" /> or <see langword="false" /> based on the next generation of the current
@ -40,27 +36,23 @@ namespace X10D
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source" /> using the <see cref="System.Random" /> instance. /// Returns a random element from <paramref name="source" /> using the <see cref="System.Random" /> instance.
/// </summary> /// </summary>
/// <typeparam name="T">The collection type.</typeparam> /// <typeparam name="T">The element type.</typeparam>
/// <param name="random">The <see cref="System.Random" /> instance.</param> /// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="source">The collection from which to draw.</param> /// <param name="source">The source collection from which to draw.</param>
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns> /// <returns>A random element of type <see cref="T" /> from <paramref name="source" />.</returns>
public static T OneOf<T>(this Random random, params T[] source)
{
return source.ToList().OneOf(random);
}
/// <summary>
/// Returns a random element from <paramref name="source" /> using the <see cref="System.Random" /> instance.
/// </summary>
/// <typeparam name="T">The collection type.</typeparam>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="source">The collection from which to draw.</param>
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="random" /> or <paramref name="source" /> is /// <paramref name="random" /> is is <see langword="null" />
/// <see langword="null" />. /// -or-
/// <paramref name="source" /> is <see langword="null" />.
/// </exception> /// </exception>
public static T OneOf<T>(this Random random, IList<T> source) /// <example>
/// <code lang="csharp">
/// var list = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
/// var random = new Random();
/// var number = random.NextFrom(list);
/// </code>
/// </example>
public static T NextFrom<T>(this System.Random random, IEnumerable<T> source)
{ {
if (random is null) if (random is null)
{ {
@ -72,7 +64,17 @@ namespace X10D
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
} }
return source[random.Next(source.Count)]; if (source is T[] array)
{
return array[random.Next(array.Length)];
}
if (source is not IReadOnlyList<T> list)
{
list = source.ToList();
}
return list[random.Next(list.Count)];
} }
} }
} }