2024-07-28 10:29:47 +00:00
|
|
|
using Pcg;
|
|
|
|
|
2024-07-20 11:51:52 +00:00
|
|
|
namespace PkmnLib.Static.Utils;
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Wrapper interface for getting random numbers.
|
|
|
|
/// </summary>
|
2024-07-20 11:51:52 +00:00
|
|
|
public interface IRandom
|
|
|
|
{
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a random integer between min and max.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="min">The minimum value (inclusive).</param>
|
|
|
|
/// <param name="max">The maximum value (exclusive).</param>
|
|
|
|
/// <returns>A random integer that is greater than or equal to min and less than max.</returns>
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt(int min, int max);
|
2024-07-20 14:12:39 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get a random integer between 0 and max.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="max">The maximum value (exclusive).</param>
|
|
|
|
/// <returns>A random integer that is greater than or equal to 0 and less than max.</returns>
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt(int max);
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a random integer between 0 and <see cref="int.MaxValue"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A random integer that is greater than or equal to 0 and less than <see cref="int.MaxValue"/>.</returns>
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt();
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a random float that is greater than or equal to 0.0 and less than 1.0.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A random float that is greater than or equal to 0.0 and less than 1.0.</returns>
|
2024-07-20 11:51:52 +00:00
|
|
|
public float GetFloat();
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a random float that is greater than or equal to min and less than max.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="min">The minimum value (inclusive).</param>
|
|
|
|
/// <param name="max">The maximum value (exclusive).</param>
|
|
|
|
/// <returns>A random float that is greater than or equal to min and less than max.</returns>
|
2024-07-20 11:51:52 +00:00
|
|
|
public float GetFloat(float min, float max);
|
2025-03-02 16:19:57 +00:00
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Get a random boolean. 50% chance of being true.
|
|
|
|
/// </summary>
|
2024-07-20 11:51:52 +00:00
|
|
|
public bool GetBool();
|
|
|
|
}
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public class RandomImpl : IRandom
|
|
|
|
{
|
2024-07-28 10:29:47 +00:00
|
|
|
private readonly PcgRandom _random;
|
2024-07-20 11:51:52 +00:00
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
2024-07-28 10:29:47 +00:00
|
|
|
/// Creates a new instance that uses the given <see cref="PcgRandom"/> instance as its source of randomness.
|
2024-07-20 14:12:39 +00:00
|
|
|
/// </summary>
|
2024-07-28 10:29:47 +00:00
|
|
|
public RandomImpl(PcgRandom random)
|
2024-07-20 11:51:52 +00:00
|
|
|
{
|
|
|
|
_random = random;
|
|
|
|
}
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
2024-07-28 10:29:47 +00:00
|
|
|
/// Creates a new instance that uses the given seed to create a new <see cref="PcgRandom"/> instance.
|
2024-07-20 14:12:39 +00:00
|
|
|
/// </summary>
|
2024-07-20 11:51:52 +00:00
|
|
|
public RandomImpl(int seed)
|
|
|
|
{
|
2024-07-28 10:29:47 +00:00
|
|
|
_random = new PcgRandom(seed);
|
2024-07-20 11:51:52 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <summary>
|
2024-07-28 10:29:47 +00:00
|
|
|
/// Creates a new instance that uses a new <see cref="PcgRandom"/> instance.
|
2024-07-20 14:12:39 +00:00
|
|
|
/// </summary>
|
2024-07-20 11:51:52 +00:00
|
|
|
public RandomImpl()
|
|
|
|
{
|
2024-07-28 10:29:47 +00:00
|
|
|
_random = new PcgRandom();
|
2024-07-20 11:51:52 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt(int min, int max) => _random.Next(min, max);
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt(int max) => _random.Next(max);
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public int GetInt() => _random.Next();
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public float GetFloat() => (float)_random.NextDouble();
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public float GetFloat(float min, float max) => (float)(_random.NextDouble() * (max - min) + min);
|
|
|
|
|
2024-07-20 14:12:39 +00:00
|
|
|
/// <inheritdoc />
|
2024-07-20 11:51:52 +00:00
|
|
|
public bool GetBool() => _random.Next(2) == 1;
|
|
|
|
}
|