🔨 Add static Random instance to RandomExtensions

To prevent creating new instances on every method call, library now
refers to a single static Random instance, when a random operation
is needed.
This commit is contained in:
Oliver Booth 2020-04-18 14:27:15 +01:00
parent f0f346056a
commit 7b06abc253
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
3 changed files with 25 additions and 11 deletions

View File

@ -18,7 +18,7 @@
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns> /// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this char[] chars, int length) public static string Random(this char[] chars, int length)
{ {
return chars.Random(length, new Random()); return chars.Random(length, RandomExtensions.Random);
} }
/// <summary> /// <summary>
@ -47,7 +47,7 @@
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns> /// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this IEnumerable<char> chars, int length) public static string Random(this IEnumerable<char> chars, int length)
{ {
return chars.Random(length, new Random()); return chars.Random(length, RandomExtensions.Random);
} }
/// <summary> /// <summary>

View File

@ -9,22 +9,36 @@
/// </summary> /// </summary>
public static class RandomExtensions public static class RandomExtensions
{ {
/// <summary>
/// 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 <paramref name="random"/>'s next /// Returns either <see langword="true"/> or <see langword="false"/> based on <paramref name="random"/>'s next
/// generation. /// generation.
/// </summary> /// </summary>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <returns>Returns <see langword="true"/> or <see langword="false"/> depending on the return value
/// from <see cref="System.Random.Next(int)"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random"/> is <see langword="null"/>.</exception>
public static bool CoinToss(this Random random) public static bool CoinToss(this Random random)
{ {
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return random.Next(2) == 0; return random.Next(2) == 0;
} }
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="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 collection type.</typeparam>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <param name="source">The collection to draw from.</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> /// <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) public static T OneOf<T>(this Random random, params T[] source)
{ {
@ -32,11 +46,11 @@
} }
/// <summary> /// <summary>
/// Returns a random element from <paramref name="source"/> using the <see cref="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 collection type.</typeparam>
/// <param name="random">The <see cref="Random"/> instance.</param> /// <param name="random">The <see cref="System.Random"/> instance.</param>
/// <param name="source">The collection to draw from.</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> /// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
public static T OneOf<T>(this Random random, IList<T> source) public static T OneOf<T>(this Random random, IList<T> source)
{ {

View File

@ -105,7 +105,7 @@
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns> /// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
public static string Random(this string str, int length) public static string Random(this string str, int length)
{ {
return str.Random(length, new Random()); return str.Random(length, RandomExtensions.Random);
} }
/// <summary> /// <summary>
@ -146,7 +146,7 @@
/// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns> /// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns>
public static string Shuffle(this string str) public static string Shuffle(this string str)
{ {
return str.Shuffle(new Random()); return str.Shuffle(RandomExtensions.Random);
} }
/// <summary> /// <summary>