1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-15 05:38:46 +02:00

also added SingleRandom ranges for floats

This commit is contained in:
Ell 2022-11-29 21:21:50 +01:00
parent 81c69041c3
commit b2b4dfbdc9
2 changed files with 52 additions and 1 deletions

View file

@ -3,7 +3,7 @@
namespace MLEM.Misc {
/// <summary>
/// The SingleRandom class allows generating single, one-off pseudorandom numbers based on a seed, or a set of seeds.
/// The types of numbers that can be generated are <see cref="int"/> and <see cref="float"/>, the former of which can be generated with specific minimum and maximum values.
/// The types of numbers that can be generated are <see cref="int"/> and <see cref="float"/>, both of which can be generated with specific minimum and maximum values if desired.
/// Methods in this class are tested to be sufficiently "random", that is, to be sufficiently distributed throughout their range, as well as sufficiently different for neighboring seeds.
/// </summary>
public class SingleRandom {
@ -94,6 +94,52 @@ namespace MLEM.Misc {
return (SingleRandom.Scramble(seeds) / (float) int.MaxValue + 1) / 2;
}
/// <summary>
/// Generates a single, one-off pseudorandom floating point number between 0 and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
/// </summary>
/// <param name="maxValue">The (exclusive) maximum value.</param>
/// <param name="seed">The seed to use.</param>
/// <returns>The generated number.</returns>
public static float Single(int maxValue, int seed) {
return maxValue * SingleRandom.Single(seed);
}
/// <summary>
/// Generates a single, one-off pseudorandom floating point number between 0 and <paramref name="maxValue"/> based on a given set of <paramref name="seeds"/>.
/// This method is guaranteed to return the same result for the same set of <paramref name="seeds"/>.
/// </summary>
/// <param name="maxValue">The (exclusive) maximum value.</param>
/// <param name="seeds">The seeds to use.</param>
/// <returns>The generated number.</returns>
public static float Single(int maxValue, params int[] seeds) {
return maxValue * SingleRandom.Single(seeds);
}
/// <summary>
/// Generates a single, one-off pseudorandom floating point number between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="seed"/>.
/// This method is guaranteed to return the same result for the same <paramref name="seed"/>.
/// </summary>
/// <param name="minValue">The (inclusive) minimum value.</param>
/// <param name="maxValue">The (exclusive) maximum value.</param>
/// <param name="seed">The seed to use.</param>
/// <returns>The generated number.</returns>
public static float Single(int minValue, int maxValue, int seed) {
return (maxValue - minValue) * SingleRandom.Single(seed) + minValue;
}
/// <summary>
/// Generates a single, one-off pseudorandom floating point number between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given set of <paramref name="seeds"/>.
/// This method is guaranteed to return the same result for the same set of <paramref name="seeds"/>.
/// </summary>
/// <param name="minValue">The (inclusive) minimum value.</param>
/// <param name="maxValue">The (exclusive) maximum value.</param>
/// <param name="seeds">The seeds to use.</param>
/// <returns>The generated number.</returns>
public static float Single(int minValue, int maxValue, params int[] seeds) {
return (maxValue - minValue) * SingleRandom.Single(seeds) + minValue;
}
private static int Scramble(int[] seeds) {
if (seeds == null || seeds.Length <= 0)
throw new ArgumentOutOfRangeException(nameof(seeds));

View file

@ -16,6 +16,8 @@ public class SingleRandomTests {
// test if all methods that accept mins and max are identical
Assert.AreEqual(SingleRandom.Int(i), SingleRandom.Int(int.MaxValue, i));
Assert.AreEqual(SingleRandom.Int(i), SingleRandom.Int(0, int.MaxValue, i));
Assert.AreEqual(SingleRandom.Single(i), SingleRandom.Single(1, i));
Assert.AreEqual(SingleRandom.Single(i), SingleRandom.Single(0, 1, i));
}
}
@ -23,6 +25,9 @@ public class SingleRandomTests {
public void TestBounds() {
for (var i = 0; i < 1000000; i++) {
Assert.That(SingleRandom.Single(i), Is.LessThan(1).And.GreaterThanOrEqualTo(0));
Assert.That(SingleRandom.Single(127, i), Is.LessThan(127).And.GreaterThanOrEqualTo(0));
Assert.That(SingleRandom.Single(12920, 1203919023, i), Is.LessThan(1203919023).And.GreaterThanOrEqualTo(12920));
Assert.That(SingleRandom.Int(i), Is.LessThan(int.MaxValue).And.GreaterThanOrEqualTo(0));
Assert.That(SingleRandom.Int(17, i), Is.LessThan(17).And.GreaterThanOrEqualTo(0));
Assert.That(SingleRandom.Int(19283, 832498394, i), Is.LessThan(832498394).And.GreaterThanOrEqualTo(19283));