mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:18:46 +00:00
🔨 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:
parent
f0f346056a
commit
7b06abc253
@ -18,7 +18,7 @@
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
public static string Random(this char[] chars, int length)
|
||||
{
|
||||
return chars.Random(length, new Random());
|
||||
return chars.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -47,7 +47,7 @@
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
public static string Random(this IEnumerable<char> chars, int length)
|
||||
{
|
||||
return chars.Random(length, new Random());
|
||||
return chars.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -9,22 +9,36 @@
|
||||
/// </summary>
|
||||
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>
|
||||
/// Returns either <see langword="true"/> or <see langword="false"/> based on <paramref name="random"/>'s next
|
||||
/// generation.
|
||||
/// </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)
|
||||
{
|
||||
if (random is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(random));
|
||||
}
|
||||
|
||||
return random.Next(2) == 0;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <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>
|
||||
public static T OneOf<T>(this Random random, params T[] source)
|
||||
{
|
||||
@ -32,11 +46,11 @@
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <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>
|
||||
public static T OneOf<T>(this Random random, IList<T> source)
|
||||
{
|
||||
|
@ -105,7 +105,7 @@
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
public static string Random(this string str, int length)
|
||||
{
|
||||
return str.Random(length, new Random());
|
||||
return str.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -146,7 +146,7 @@
|
||||
/// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns>
|
||||
public static string Shuffle(this string str)
|
||||
{
|
||||
return str.Shuffle(new Random());
|
||||
return str.Shuffle(RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user