mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 03:45:41 +00:00
Add Random.Next<T>() where T : Enum
This commit is contained in:
parent
fc96840e9d
commit
a401aabee8
@ -11,6 +11,27 @@ namespace X10D.RandomExtensions
|
|||||||
{
|
{
|
||||||
internal static readonly Random Random = new();
|
internal static readonly Random Random = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a random value that defined in a specified enum.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||||
|
/// <typeparam name="T">An enum type.</typeparam>
|
||||||
|
/// <returns>
|
||||||
|
/// A <typeparamref name="T" /> value at index <c>n</c> where <c>n = </c><see cref="System.Random.Next(int)" />.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||||
|
public static T Next<T>(this Random random)
|
||||||
|
where T : struct, Enum
|
||||||
|
{
|
||||||
|
if (random is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(random));
|
||||||
|
}
|
||||||
|
|
||||||
|
var values = Enum.GetValues(typeof(T));
|
||||||
|
return (T)values.GetValue(random.Next(values.Length))!;
|
||||||
|
}
|
||||||
|
|
||||||
/// <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
|
||||||
/// <see cref="System.Random" />.
|
/// <see cref="System.Random" />.
|
||||||
|
Loading…
Reference in New Issue
Block a user