diff --git a/X10D/src/ListExtensions.cs b/X10D/src/ListExtensions.cs index 5f6cb0d..48389c3 100644 --- a/X10D/src/ListExtensions.cs +++ b/X10D/src/ListExtensions.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace X10D { @@ -10,51 +9,34 @@ namespace X10D public static class ListExtensions { /// - /// Returns a random element from using a new instance. + /// Returns a random element from the current list using a specified instance. /// - /// The collection type. - /// The collection to draw from. - /// Returns a random element of type from . - public static T OneOf(this IEnumerable source) + /// The element type. + /// The source collection from which to draw. + /// The instance. + /// A random element of type from . + /// + /// is is + /// -or- + /// is . + /// + /// + /// + /// var list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + /// var number = list.Random(); + /// + /// + public static T Random(this IReadOnlyList source, System.Random? random = null) { - return source.OneOf(new Random()); - } - - /// - /// Returns a random element from using the instance. - /// - /// The collection type. - /// The collection to draw from. - /// The instance. - /// Returns a random element of type from . - public static T OneOf(this IEnumerable source, Random random) - { - return source.ToList().OneOf(random); - } - - /// - /// Returns a random element from using a new instance. - /// - /// The collection type. - /// The collection to draw from. - /// Returns a random element of type from . - public static T OneOf(this IList source) - { - return source.OneOf(new Random()); - } - - /// - /// Returns a random element from using the instance. - /// - /// The collection type. - /// The collection to draw from. - /// The instance. - /// Returns a random element of type from . - public static T OneOf(this IList source, Random random) - { - return random.OneOf(source); - } + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } + random ??= RandomExtensions.Random; + return random.NextFrom(source); + } + /// /// Reorganizes the elements in a list by implementing a Fisher-Yates shuffle. ///