2022-11-30 23:11:37 +01:00
namespace MLEM.Misc {
2022-11-29 21:11:59 +01:00
/// <summary>
2022-11-30 20:21:24 +01:00
/// The SingleRandom class allows generating single, one-off pseudorandom numbers based on a seed or a <see cref="SeedSource"/>.
2022-11-29 21:21:50 +01:00
/// 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.
2022-11-29 21:11:59 +01:00
/// 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 {
/// <summary>
/// Generates a single, one-off pseudorandom integer between 0 and <see cref="int.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="seed">The seed to use.</param>
/// <returns>The generated number.</returns>
public static int Int ( int seed ) {
return ( int ) ( SingleRandom . Single ( seed ) * int . MaxValue ) ;
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom integer between 0 and <see cref="int.MaxValue"/> based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:11:59 +01:00
/// </summary>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:11:59 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static int Int ( SeedSource source ) {
return ( int ) ( SingleRandom . Single ( source ) * int . MaxValue ) ;
2022-11-29 21:11:59 +01:00
}
/// <summary>
/// Generates a single, one-off pseudorandom integer 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 int Int ( int maxValue , int seed ) {
return ( int ) ( maxValue * SingleRandom . Single ( seed ) ) ;
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom integer between 0 and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:11:59 +01:00
/// </summary>
/// <param name="maxValue">The (exclusive) maximum value.</param>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:11:59 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static int Int ( int maxValue , SeedSource source ) {
return ( int ) ( maxValue * SingleRandom . Single ( source ) ) ;
2022-11-29 21:11:59 +01:00
}
/// <summary>
/// Generates a single, one-off pseudorandom integer 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 int Int ( int minValue , int maxValue , int seed ) {
return ( int ) ( ( maxValue - minValue ) * SingleRandom . Single ( seed ) ) + minValue ;
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom integer between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:11:59 +01:00
/// </summary>
/// <param name="minValue">The (inclusive) minimum value.</param>
/// <param name="maxValue">The (exclusive) maximum value.</param>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:11:59 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static int Int ( int minValue , int maxValue , SeedSource source ) {
return ( int ) ( ( maxValue - minValue ) * SingleRandom . Single ( source ) ) + minValue ;
2022-11-29 21:11:59 +01:00
}
/// <summary>
/// Generates a single, one-off pseudorandom floating point number between 0 and 1 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="seed">The seed to use.</param>
/// <returns>The generated number.</returns>
public static float Single ( int seed ) {
2022-12-04 22:43:25 +01:00
return ( new SeedSource ( ) . Add ( seed ) . Get ( ) / ( float ) int . MaxValue + 1 ) / 2 ;
2022-11-29 21:11:59 +01:00
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:11:59 +01:00
/// </summary>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:11:59 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static float Single ( SeedSource source ) {
return ( source . Get ( ) / ( float ) int . MaxValue + 1 ) / 2 ;
2022-11-29 21:11:59 +01:00
}
2022-11-29 21:21:50 +01:00
/// <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>
2022-11-29 21:29:53 +01:00
public static float Single ( float maxValue , int seed ) {
2022-11-29 21:21:50 +01:00
return maxValue * SingleRandom . Single ( seed ) ;
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom floating point number between 0 and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:21:50 +01:00
/// </summary>
/// <param name="maxValue">The (exclusive) maximum value.</param>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:21:50 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static float Single ( float maxValue , SeedSource source ) {
return maxValue * SingleRandom . Single ( source ) ;
2022-11-29 21:21:50 +01:00
}
/// <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>
2022-11-29 21:29:53 +01:00
public static float Single ( float minValue , float maxValue , int seed ) {
2022-11-29 21:21:50 +01:00
return ( maxValue - minValue ) * SingleRandom . Single ( seed ) + minValue ;
}
/// <summary>
2022-11-30 20:21:24 +01:00
/// Generates a single, one-off pseudorandom floating point number between <paramref name="minValue"/> and <paramref name="maxValue"/> based on a given <paramref name="source"/>.
/// This method is guaranteed to return the same result for the same <paramref name="source"/>.
2022-11-29 21:21:50 +01:00
/// </summary>
/// <param name="minValue">The (inclusive) minimum value.</param>
/// <param name="maxValue">The (exclusive) maximum value.</param>
2022-11-30 20:21:24 +01:00
/// <param name="source">The <see cref="SeedSource"/> to use.</param>
2022-11-29 21:21:50 +01:00
/// <returns>The generated number.</returns>
2022-11-30 20:21:24 +01:00
public static float Single ( float minValue , float maxValue , SeedSource source ) {
return ( maxValue - minValue ) * SingleRandom . Single ( source ) + minValue ;
2022-11-29 21:21:50 +01:00
}
2022-11-30 20:21:24 +01:00
}
2022-11-29 21:11:59 +01:00
}