mirror of
https://github.com/NuclearPowered/Reactor.git
synced 2026-10-19 15:51:54 +00:00
RandomExtensions
This commit is contained in:
1 file changed
+23
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user