namespace MLEM.Misc { /// /// The SingleRandom class allows generating single, one-off pseudorandom numbers based on a seed or a . /// The types of numbers that can be generated are and , 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. /// public class SingleRandom { /// /// Generates a single, one-off pseudorandom integer between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The seed to use. /// The generated number. public static int Int(int seed) { return (int) (SingleRandom.Single(seed) * int.MaxValue); } /// /// Generates a single, one-off pseudorandom integer between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The to use. /// The generated number. public static int Int(SeedSource source) { return (int) (SingleRandom.Single(source) * int.MaxValue); } /// /// Generates a single, one-off pseudorandom integer between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (exclusive) maximum value. /// The seed to use. /// The generated number. public static int Int(int maxValue, int seed) { return (int) (maxValue * SingleRandom.Single(seed)); } /// /// Generates a single, one-off pseudorandom integer between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (exclusive) maximum value. /// The to use. /// The generated number. public static int Int(int maxValue, SeedSource source) { return (int) (maxValue * SingleRandom.Single(source)); } /// /// Generates a single, one-off pseudorandom integer between and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (inclusive) minimum value. /// The (exclusive) maximum value. /// The seed to use. /// The generated number. public static int Int(int minValue, int maxValue, int seed) { return (int) ((maxValue - minValue) * SingleRandom.Single(seed)) + minValue; } /// /// Generates a single, one-off pseudorandom integer between and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (inclusive) minimum value. /// The (exclusive) maximum value. /// The to use. /// The generated number. public static int Int(int minValue, int maxValue, SeedSource source) { return (int) ((maxValue - minValue) * SingleRandom.Single(source)) + minValue; } /// /// Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given . /// This method is guaranteed to return the same result for the same . /// /// The seed to use. /// The generated number. public static float Single(int seed) { return (new SeedSource().Add(seed).Get() / (float) int.MaxValue + 1) / 2; } /// /// Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given . /// This method is guaranteed to return the same result for the same . /// /// The to use. /// The generated number. public static float Single(SeedSource source) { return (source.Get() / (float) int.MaxValue + 1) / 2; } /// /// Generates a single, one-off pseudorandom floating point number between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (exclusive) maximum value. /// The seed to use. /// The generated number. public static float Single(float maxValue, int seed) { return maxValue * SingleRandom.Single(seed); } /// /// Generates a single, one-off pseudorandom floating point number between 0 and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (exclusive) maximum value. /// The to use. /// The generated number. public static float Single(float maxValue, SeedSource source) { return maxValue * SingleRandom.Single(source); } /// /// Generates a single, one-off pseudorandom floating point number between and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (inclusive) minimum value. /// The (exclusive) maximum value. /// The seed to use. /// The generated number. public static float Single(float minValue, float maxValue, int seed) { return (maxValue - minValue) * SingleRandom.Single(seed) + minValue; } /// /// Generates a single, one-off pseudorandom floating point number between and based on a given . /// This method is guaranteed to return the same result for the same . /// /// The (inclusive) minimum value. /// The (exclusive) maximum value. /// The to use. /// The generated number. public static float Single(float minValue, float maxValue, SeedSource source) { return (maxValue - minValue) * SingleRandom.Single(source) + minValue; } } }