ace/Source/ACE.Common/ThreadSafeRandom.cs
fartwhif fe34acc14f Moving class ACE.Server.Physics.Common.Random to ACE.ThreadSafeRandom in project ACE.Common (#1167)
* Moving ACE.Server.Physics.Common.Random to ACE.ThreadSafeRandom
replaced the thread discriminator with a lock
renamed "Random" to "ThreadSafeRandom"
renamed "RollDice" functions to the standard "Next"

* deleted Source/ACE.Server/Physics/Common/Random.cs
replaced conditionally omitted and commented code with new name

* renamed source file
2018-12-21 23:51:57 -05:00

42 lines
1.3 KiB
C#

namespace ACE
{
// important class, ensure unit tests pass for this
public static class ThreadSafeRandom
{
private static readonly object randomMutex = new object();
private static readonly System.Random random = new System.Random();
/// <summary>
/// Returns a random number between min and max
/// </summary>
public static float Next(float min, float max)
{
// todo: implement exactly the way AC handles it
// inclusive/exclusive?
lock (randomMutex)
{
return (float)(random.NextDouble() * (max - min) + min);
}
}
/// <summary>
/// Returns a random integer between min and max, inclusive
/// </summary>
/// <param name="min">The minimum possible value to return</param>
/// <param name="max">The maximum possible value to return</param>
public static int Next(int min, int max)
{
lock (randomMutex)
{
return random.Next(min, max + 1);
}
}
public static uint Next(uint min, uint max)
{
lock (randomMutex)
{
return (uint)random.Next((int)min, (int)(max + 1));
}
}
}
}