Random timespan support (#3458)

This commit is contained in:
metalgearsloth
2022-11-11 09:32:52 +11:00
committed by GitHub
parent cf5c72e7ea
commit 81cd43f988
2 changed files with 17 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Robust.Shared.Maths;
@@ -11,6 +12,8 @@ namespace Robust.Shared.Random
public float NextFloat(float maxValue) => NextFloat() * maxValue;
int Next();
int Next(int minValue, int maxValue);
TimeSpan Next(TimeSpan minTime, TimeSpan maxTime);
TimeSpan Next(TimeSpan maxTime);
int Next(int maxValue);
double NextDouble();
double NextDouble(double minValue, double maxValue) => NextDouble() * (maxValue - minValue) + minValue;

View File

@@ -1,3 +1,6 @@
using System;
using Robust.Shared.Utility;
namespace Robust.Shared.Random
{
public sealed class RobustRandom : IRobustRandom
@@ -19,6 +22,17 @@ namespace Robust.Shared.Random
return _random.Next(minValue, maxValue);
}
public TimeSpan Next(TimeSpan minTime, TimeSpan maxTime)
{
DebugTools.Assert(minTime < maxTime);
return minTime + (maxTime - minTime) * _random.NextDouble();
}
public TimeSpan Next(TimeSpan maxTime)
{
return Next(TimeSpan.Zero, maxTime);
}
public int Next(int maxValue)
{
return _random.Next(maxValue);