Slightly improve the random byte generation code

This commit is contained in:
stefnotch
2021-04-12 10:46:00 +02:00
parent 27400cffe4
commit a58645da0f

View File

@@ -211,6 +211,17 @@ namespace FlaxEngine.Utilities
/// <returns>A <see cref="bool"/> thats either true or false.</returns>
public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight;
/// <summary>
/// Generates a random <see cref="byte"/> value up until an exclusive maximum.
/// </summary>
/// <param name="random">An instance of <see cref="Random"/>.</param>
/// <param name="max">The maximum value. If it's zero, a maximum of 256 is used</param>
/// <returns>A random <see cref="byte"/> between min and max.</returns>
public static byte NextByte(this Random random, byte max = 0)
{
return max == 0 ? (byte)(random.Next() % 256) : (byte)random.Next(max);
}
/// <summary>
/// Generates a random <see cref="byte"/> value between min and max.
/// </summary>
@@ -218,9 +229,9 @@ namespace FlaxEngine.Utilities
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>A random <see cref="byte"/> between min and max.</returns>
public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue)
public static byte NextByte(this Random random, byte min, byte max)
{
return (byte)random.Next(min, max == byte.MaxValue ? byte.MaxValue + 1 : max);
return (byte)random.Next(min, max);
}
/// <summary>