//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.GameLogic;
///
/// A static class to provide random functions by encapsulating the default Randomizer .
///
public static class Rand
{
private static readonly IRandomizer Randomizer = new SimpleRandomizer();
[ThreadStatic]
private static Random? _randomInstance;
private static Random RandomInstance => _randomInstance ??= new Random();
///
/// Gets the default Randomizer which is implementing the interface .
///
/// The default Randomizer which is implementing the interface .
public static IRandomizer GetRandomizer()
{
return Randomizer;
}
///
/// Returns a random boolean value.
///
/// Random boolean value.
public static bool NextRandomBool()
{
int a = RandomInstance.Next(0, 2);
return a == 1;
}
///
/// Returns a random boolean value, with percent of a chance to be true.
///
/// Percentage of true.
/// Random boolean value.
public static bool NextRandomBool(int percent)
{
if (percent == 0)
{
return false;
}
if (percent == 100)
{
return true;
}
int a = RandomInstance.Next(0, 100);
return a <= percent;
}
///
/// Returns a random boolean value, with between 0 and 1 to be true.
///
/// Chance between 0 and 1 to be true.
/// Random boolean value.
public static bool NextRandomBool(double chance)
{
if (chance == 0.0)
{
return false;
}
var lot = RandomInstance.NextDouble();
return lot <= chance;
}
///
/// Returns a random boolean value with the chance of .
///
/// Chance to be true.
/// Base of the chance.
/// Random boolean value.
public static bool NextRandomBool(int chance, int basis)
{
int a = RandomInstance.Next(0, basis);
return a <= chance;
}
///
/// Returns a random integer between min and max.
///
/// The inclusive lower bound of the random number returned.
/// The exclusive upper bound of the random number returned.
/// A random number.
public static int NextInt(int min, int max)
{
return RandomInstance.Next(min, max);
}
///
/// Returns a random integer between min and max.
///
/// The inclusive lower bound of the random number returned.
/// The exclusive upper bound of the random number returned.
/// A random number.
public static int NextInt(uint min, uint max)
{
return RandomInstance.Next((int)min, (int)max);
}
///
/// Returns a random integer between min and max.
///
/// The inclusive lower bound of the random number returned.
/// The exclusive upper bound of the random number returned.
/// A random number.
public static uint NextUInt(uint min, uint max)
{
return (uint)RandomInstance.Next((int)System.Math.Max(min, 0), (int)max);
}
///
/// Returns a random double between 0 and 1.
///
/// A random double between 0 and 1.
public static double NextDouble()
{
return RandomInstance.NextDouble();
}
private class SimpleRandomizer : IRandomizer
{
bool IRandomizer.NextRandomBool()
{
return Rand.NextRandomBool();
}
bool IRandomizer.NextRandomBool(int percent)
{
return Rand.NextRandomBool(percent);
}
bool IRandomizer.NextRandomBool(int chance, int basis)
{
return Rand.NextRandomBool(chance, basis);
}
bool IRandomizer.NextRandomBool(double chance)
{
return Rand.NextRandomBool(chance);
}
int IRandomizer.NextInt(int min, int max)
{
return Rand.NextInt(min, max);
}
int IRandomizer.NextInt(uint min, uint max)
{
return Rand.NextInt(min, max);
}
uint IRandomizer.NextUInt(uint min, uint max)
{
return Rand.NextUInt(min, max);
}
double IRandomizer.NextDouble()
{
return Rand.NextDouble();
}
}
}