From 51ba5b01154fc1c856aca21006c34b324592ce71 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 18 Jan 2021 15:19:59 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=9A=20Rename=20List.OneOf=20to=20List.?= =?UTF-8?q?Random?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Item for #21 --- X10D/src/ListExtensions.cs | 68 ++++++++++++++------------------------ 1 file changed, 25 insertions(+), 43 deletions(-) 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. ///