mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-15 14:52:35 +02:00
* refactor: RobustRandom and RandomExtensions namespace change to file-scoped * refactor: IRobustRandom xml-doc methods rearranged to be more structured. * feat: GetItems methods added to RandomExtensions, tests for new methods added. * fix: GetItems will not request count-1 from next random, as System.Random.Next have upper bound excluded. * fix: enforced standard deviation on picking next items in RandomExtensions.GetItems + fixed hashet initial capacity +removed mandatory hashset allocation * refactor: specified border values interaction in IRobustRandom xml-doc * refactor: updated relese-notes * refactor: changed release-notes PROPERLY * fix: order by which unique random items are picked in RandomExtensions.GetItems were fixed to ACTUALLY follow normal distribution * refractor: added comment for devious RandomExtensions.GetItems only-unique logic * reduce code duplication * Cleanup code a bit Rename variables, and make it a bit more compact. Also, IMO the description is unnecessary * Remove obsolete extension * Remove incorrect O(n) comments. --------- Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
66 lines
1.5 KiB
C#
66 lines
1.5 KiB
C#
using System;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Robust.Shared.Random;
|
|
|
|
/// <summary>
|
|
/// Wrapper for <see cref="Random"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This should not contain any logic, not directly related to calling specific methods of <see cref="Random"/>.
|
|
/// To write additional logic, attached to random roll, please create interface-implemented methods on <see cref="IRobustRandom"/>
|
|
/// or add it to <see cref="RandomExtensions"/>.
|
|
/// </remarks>
|
|
public sealed class RobustRandom : IRobustRandom
|
|
{
|
|
private System.Random _random = new();
|
|
|
|
public System.Random GetRandom() => _random;
|
|
|
|
public void SetSeed(int seed)
|
|
{
|
|
_random = new(seed);
|
|
}
|
|
|
|
public float NextFloat()
|
|
{
|
|
return _random.NextFloat();
|
|
}
|
|
|
|
public int Next()
|
|
{
|
|
return _random.Next();
|
|
}
|
|
|
|
public int Next(int minValue, int maxValue)
|
|
{
|
|
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);
|
|
}
|
|
|
|
public double NextDouble()
|
|
{
|
|
return _random.NextDouble();
|
|
}
|
|
|
|
public void NextBytes(byte[] buffer)
|
|
{
|
|
_random.NextBytes(buffer);
|
|
}
|
|
}
|