mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Deprecate System.Random leakage
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
@@ -12,10 +13,12 @@ namespace Robust.Shared.Map
|
||||
{
|
||||
Tile GetVariantTile(string name, IRobustRandom random);
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
Tile GetVariantTile(string name, System.Random random);
|
||||
|
||||
Tile GetVariantTile(ITileDefinition tileDef, IRobustRandom random);
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
Tile GetVariantTile(ITileDefinition tileDef, System.Random random);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -176,8 +176,10 @@ public interface IRobustRandom
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static class RandomHelpers
|
||||
{
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static void Shuffle<T>(this System.Random random, IList<T> list)
|
||||
{
|
||||
var n = list.Count;
|
||||
@@ -189,24 +191,28 @@ public static class RandomHelpers
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static bool Prob(this System.Random random, double chance)
|
||||
{
|
||||
return random.NextDouble() < chance;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static byte NextByte(this System.Random random, byte maxValue)
|
||||
{
|
||||
return NextByte(random, 0, maxValue);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static byte NextByte(this System.Random random)
|
||||
{
|
||||
return NextByte(random, byte.MaxValue);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static byte NextByte(this System.Random random, byte minValue, byte maxValue)
|
||||
{
|
||||
return (byte)random.Next(minValue, maxValue);
|
||||
|
||||
@@ -19,7 +19,13 @@ public static class RandomExtensions
|
||||
/// <param name="σ">The standard deviation of the normal distribution.</param>
|
||||
public static double NextGaussian(this IRobustRandom random, double μ = 0, double σ = 1)
|
||||
{
|
||||
return random.GetRandom().NextGaussian(μ, σ);
|
||||
// https://stackoverflow.com/a/218600
|
||||
var α = random.NextDouble();
|
||||
var β = random.NextDouble();
|
||||
|
||||
var randStdNormal = Math.Sqrt(-2.0 * Math.Log(α)) * Math.Sin(2.0 * Math.PI * β);
|
||||
|
||||
return μ + σ * randStdNormal;
|
||||
}
|
||||
|
||||
/// <summary>Picks a random element from a collection.</summary>
|
||||
@@ -78,6 +84,7 @@ public static class RandomExtensions
|
||||
/// Picks a random element from a set and returns it.
|
||||
/// This is O(n) as it has to iterate the collection until the target index.
|
||||
/// </summary>
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static T Pick<T>(this System.Random random, ICollection<T> collection)
|
||||
{
|
||||
var index = random.Next(collection.Count);
|
||||
@@ -97,6 +104,7 @@ public static class RandomExtensions
|
||||
/// Picks a random from a collection then removes it and returns it.
|
||||
/// This is O(n) as it has to iterate the collection until the target index.
|
||||
/// </summary>
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static T PickAndTake<T>(this System.Random random, ICollection<T> set)
|
||||
{
|
||||
var tile = Pick(random, set);
|
||||
@@ -110,6 +118,7 @@ public static class RandomExtensions
|
||||
/// <param name="random">The random object to generate the number from.</param>
|
||||
/// <param name="μ">The average or "center" of the normal distribution.</param>
|
||||
/// <param name="σ">The standard deviation of the normal distribution.</param>
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static double NextGaussian(this System.Random random, double μ = 0, double σ = 1)
|
||||
{
|
||||
// https://stackoverflow.com/a/218600
|
||||
@@ -121,17 +130,21 @@ public static class RandomExtensions
|
||||
return μ + σ * randStdNormal;
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static Angle NextAngle(this System.Random random) => NextFloat(random) * MathF.Tau;
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static Angle NextAngle(this System.Random random, Angle minAngle, Angle maxAngle)
|
||||
{
|
||||
DebugTools.Assert(minAngle < maxAngle);
|
||||
return minAngle + (maxAngle - minAngle) * random.NextDouble();
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static Vector2 NextPolarVector2(this System.Random random, float minMagnitude, float maxMagnitude)
|
||||
=> random.NextAngle().RotateVec(new Vector2(random.NextFloat(minMagnitude, maxMagnitude), 0));
|
||||
|
||||
[Obsolete("Exists as a method directly on IRobustRandom.")]
|
||||
public static float NextFloat(this IRobustRandom random)
|
||||
{
|
||||
// This is pretty much the CoreFX implementation.
|
||||
@@ -140,11 +153,13 @@ public static class RandomExtensions
|
||||
return random.Next() * 4.6566128752458E-10f;
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static float NextFloat(this System.Random random)
|
||||
{
|
||||
return random.Next() * 4.6566128752458E-10f;
|
||||
}
|
||||
|
||||
[Obsolete("Always use RobustRandom/IRobustRandom, System.Random does not provide any extra functionality.")]
|
||||
public static float NextFloat(this System.Random random, float minValue, float maxValue)
|
||||
=> random.NextFloat() * (maxValue - minValue) + minValue;
|
||||
|
||||
|
||||
@@ -4,15 +4,23 @@ using Robust.Shared.Utility;
|
||||
namespace Robust.Shared.Random;
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for <see cref="Random"/>.
|
||||
/// Provides random numbers, can be constructed in user code or used as a dependency in the form of
|
||||
/// <see cref="IRobustRandom"/>. Methods that take RNG as input should take an IRobustRandom instead.
|
||||
/// </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>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// var myRng = new RobustRandom();
|
||||
/// // Optionally, seed your RNG. By default, the RNG is seeded randomly.
|
||||
/// myRng.SetSeed(17);
|
||||
/// <br/>
|
||||
/// var fairDiceRoll = myRng.Next(1, 6); // Will be 4 with this seed.
|
||||
/// </code>
|
||||
/// </example>
|
||||
public sealed class RobustRandom : IRobustRandom
|
||||
{
|
||||
// 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"/>.
|
||||
private System.Random _random = new();
|
||||
|
||||
public System.Random GetRandom() => _random;
|
||||
@@ -24,7 +32,10 @@ public sealed class RobustRandom : IRobustRandom
|
||||
|
||||
public float NextFloat()
|
||||
{
|
||||
return _random.NextFloat();
|
||||
// This is pretty much the CoreFX implementation.
|
||||
// So credits to that.
|
||||
// Except using float instead of double.
|
||||
return Next() * 4.6566128752458E-10f;
|
||||
}
|
||||
|
||||
public int Next()
|
||||
|
||||
Reference in New Issue
Block a user