RandomExtensions

This commit is contained in:
js6pak committed 2021-12-14 22:40:12 +01:00
1 parent c96f12029e
commit f501dc1897
1 file changed
+23
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Reactor.Extensions;
public static class RandomExtensions
{
/// <summary>
/// Returns random <typeparamref name="T"/> from <paramref name="input"/> using <paramref name="random"/>
/// </summary>
public static T? Random<T>(this IEnumerable<T> input, Random random)
{
var list = input as IList<T> ?? input.ToList();
return list.Count == 0 ? default : list[random.Next(0, list.Count)];
}
/// <summary>Returns a random double that is within a specified range.</summary>
public static double NextDouble(this Random random, double minValue, double maxValue)
{
return random.NextDouble() * (maxValue - minValue) + minValue;
}
}