From a58645da0f57d2b9f4e2afcde0ba68096dbc3f3b Mon Sep 17 00:00:00 2001 From: stefnotch Date: Mon, 12 Apr 2021 10:46:00 +0200 Subject: [PATCH] Slightly improve the random byte generation code --- Source/Engine/Utilities/Extensions.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index b3f6d2053..3eb31e1d2 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -211,6 +211,17 @@ namespace FlaxEngine.Utilities /// A thats either true or false. public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight; + /// + /// Generates a random value up until an exclusive maximum. + /// + /// An instance of . + /// The maximum value. If it's zero, a maximum of 256 is used + /// A random between min and max. + public static byte NextByte(this Random random, byte max = 0) + { + return max == 0 ? (byte)(random.Next() % 256) : (byte)random.Next(max); + } + /// /// Generates a random value between min and max. /// @@ -218,9 +229,9 @@ namespace FlaxEngine.Utilities /// The minimum value. /// The maximum value. /// A random between min and max. - 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); } ///