From eaba489ba28dd0aaafedf87d6d9e2d570fcea924 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Tue, 12 Jan 2021 15:08:45 +0100 Subject: [PATCH 01/13] Initial Random class First Commit Static class that inhabits basic and common random functions, allows it to be used in visual scripting as well. --- Source/Engine/Core/Math/Randomf.cs | 143 +++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Source/Engine/Core/Math/Randomf.cs diff --git a/Source/Engine/Core/Math/Randomf.cs b/Source/Engine/Core/Math/Randomf.cs new file mode 100644 index 000000000..791de2cb8 --- /dev/null +++ b/Source/Engine/Core/Math/Randomf.cs @@ -0,0 +1,143 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using System; + +namespace FlaxEngine +{ + /// + /// A collection of common random functions which supports various types. + /// + public static class Randomf + { + // Single instance for random + private static Random _rand = new Random(0); + + /// + /// Sets the seed of the random number generator. + /// Causes to construct a new instance. + /// + /// The new seed + public static void SetSeed(int newSeed) => _rand = new Random(newSeed); + + // Bool + + public static bool RandomBool() + { + return RandomInt() == 1 ? true : false; + } + + public static bool RandomBoolWithWeight(float weight = 0) + { + return weight >= RandomInt() ? true : false; + } + + // Byte + + public static byte RandomByte(byte min = 0, byte max = 1) + { + return (byte)_rand.Next(min, max+1); + } + + public static byte RandomUniformByte(byte value = 1) + { + int max = Math.Abs(value); + int min = max * -1; + + return (byte)_rand.Next(min, max); + } + + // Int + + public static int RandomInt(int min = 0, int max = 1) + { + return _rand.Next(min, max+1); + } + + public static int RandomUniformInt(int value = 1) + { + int max = Math.Abs(value); + int min = max * -1; + + return _rand.Next(min, max); + } + + // Float + + public static float RandomFloat(float min = 0.0f, float max = 1.0f) + { + return (float)_rand.NextDouble() * (max - min) + min; + } + + public static float RandomUniformFloat(float value = 1.0f) + { + // Ensure that in case of negative values we still return a value between + value and - value. + float max = Math.Abs(value); + float min = max * -1; + + return (float)_rand.NextDouble() * (max - min) + min; + } + + // Quaternion + + public static Quaternion RandomQuaternion() + { + return Quaternion.Euler( + RandomUniformFloat(180), + RandomUniformFloat(180), + RandomUniformFloat(180) + ); + } + + // Vector + + public static Vector3 RandomVector3() + { + Vector3 output; + + do + { + // Create random float with a mean of 0 and deviation of ±1; + output.X = RandomFloat() * 2.0f - 1.0f; + output.Y = RandomFloat() * 2.0f - 1.0f; + output.Z = RandomFloat() * 2.0f - 1.0f; + + } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); + + output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); + + return output; + } + + public static Vector2 RandomVector2() + { + Vector2 output; + + do + { + // Create random float with a mean of 0 and deviation of +-1; + output.X = RandomFloat() * 2.0f - 1.0f; + output.Y = RandomFloat() * 2.0f - 1.0f; + + } while (output.LengthSquared > 1 || output.LengthSquared< 1e-6f); + + output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); + + return output; + } + + // Color + + public static Color RandomColor(bool trueRandom, bool randomAlpha) + { + float alpha = randomAlpha ? RandomFloat() : 1f; + + if (!trueRandom) + return Color.FromHSV(RandomFloat(0f, 360f), 1f, 1f, alpha); + + return new Color( + RandomFloat(0f, 255f), + RandomFloat(0f, 255f), + RandomFloat(0f, 255f), alpha); + } + } +} From 783b9107009384e8958a6a4a2a78577b333b5eb5 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Tue, 12 Jan 2021 20:29:32 +0100 Subject: [PATCH 02/13] Added XML comments --- Source/Engine/Core/Math/Randomf.cs | 94 +++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 27 deletions(-) diff --git a/Source/Engine/Core/Math/Randomf.cs b/Source/Engine/Core/Math/Randomf.cs index 791de2cb8..9e25456ab 100644 --- a/Source/Engine/Core/Math/Randomf.cs +++ b/Source/Engine/Core/Math/Randomf.cs @@ -12,32 +12,44 @@ namespace FlaxEngine // Single instance for random private static Random _rand = new Random(0); + //TODO: Implement some form of generic method for numerics? + /// /// Sets the seed of the random number generator. - /// Causes to construct a new instance. + /// Constructs a new instance. /// - /// The new seed + /// The new seed. public static void SetSeed(int newSeed) => _rand = new Random(newSeed); - // Bool + /// + /// Generates a random . + /// + /// A thats either true or false. + public static bool RandomBool() => RandomInt() == 1; - public static bool RandomBool() - { - return RandomInt() == 1 ? true : false; - } - - public static bool RandomBoolWithWeight(float weight = 0) - { - return weight >= RandomInt() ? true : false; - } - - // Byte + /// + /// Generates a random with a weight value to adjust preference. + /// + /// Normalized value that determines the chance to return true. + /// A thats either true or false. + public static bool RandomBoolWithWeight(float weight = 0) => weight >= RandomFloat(); + /// + /// Generates a random value between min and max. + /// + /// The lower boundary. + /// The upper boundary. + /// A between min and max. public static byte RandomByte(byte min = 0, byte max = 1) { return (byte)_rand.Next(min, max+1); } + /// + /// Generates a random by using a single value as both upper and lower boundary. + /// + /// Defines both upper and lower boundary. + /// A that is between +value and -value. public static byte RandomUniformByte(byte value = 1) { int max = Math.Abs(value); @@ -46,13 +58,22 @@ namespace FlaxEngine return (byte)_rand.Next(min, max); } - // Int - + /// + /// Generates a random value between min and max. + /// + /// The lower boundary. + /// The upper boundary. + /// A random between min and max. public static int RandomInt(int min = 0, int max = 1) { return _rand.Next(min, max+1); } + /// + /// Generates a random by using a single value as both upper and lower boundary. + /// + /// Defines both upper and lower boundary. + /// A random between +value and -value. public static int RandomUniformInt(int value = 1) { int max = Math.Abs(value); @@ -61,13 +82,22 @@ namespace FlaxEngine return _rand.Next(min, max); } - // Float - + /// + /// Generates a random value between min and max. + /// + /// The lower boundary. + /// The upper boundary. + /// A random between min and max. public static float RandomFloat(float min = 0.0f, float max = 1.0f) { return (float)_rand.NextDouble() * (max - min) + min; } + /// + /// Generates a random by using a single value as both upper and lower boundary. + /// + /// Defines both upper and lower boundary. + /// A random between +value and -value. public static float RandomUniformFloat(float value = 1.0f) { // Ensure that in case of negative values we still return a value between + value and - value. @@ -77,19 +107,22 @@ namespace FlaxEngine return (float)_rand.NextDouble() * (max - min) + min; } - // Quaternion - + /// + /// Generates a random . + /// + /// A random . public static Quaternion RandomQuaternion() { return Quaternion.Euler( RandomUniformFloat(180), RandomUniformFloat(180), - RandomUniformFloat(180) - ); + RandomUniformFloat(180)); } - // Vector - + /// + /// Generates a uniformly distributed random unit length vector point on a unit sphere. + /// + /// A random . public static Vector3 RandomVector3() { Vector3 output; @@ -108,13 +141,16 @@ namespace FlaxEngine return output; } + /// + /// Generates a uniformly distributed random unit length vector point on a unit sphere in 2D. + /// + /// A random public static Vector2 RandomVector2() { Vector2 output; do { - // Create random float with a mean of 0 and deviation of +-1; output.X = RandomFloat() * 2.0f - 1.0f; output.Y = RandomFloat() * 2.0f - 1.0f; @@ -125,8 +161,12 @@ namespace FlaxEngine return output; } - // Color - + /// + /// Generates a random color. + /// + /// Should the color be generated from a single random Hue value or separate values for each channel. + /// Randomize the alpha value. + /// A nice random color. public static Color RandomColor(bool trueRandom, bool randomAlpha) { float alpha = randomAlpha ? RandomFloat() : 1f; From 604c583c36c3da255c28da2dd5d797d64c1b5202 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 13 Jan 2021 21:16:17 +0100 Subject: [PATCH 03/13] Moved all methods to Extension.cs Moved the methods for RandomUtils.cs to Extension.cs instead. --- Source/Engine/Core/Math/Randomf.cs | 183 ------------- Source/Engine/Utilities/Extensions.cs | 355 ++++++++++++++------------ 2 files changed, 189 insertions(+), 349 deletions(-) delete mode 100644 Source/Engine/Core/Math/Randomf.cs diff --git a/Source/Engine/Core/Math/Randomf.cs b/Source/Engine/Core/Math/Randomf.cs deleted file mode 100644 index 9e25456ab..000000000 --- a/Source/Engine/Core/Math/Randomf.cs +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -using System; - -namespace FlaxEngine -{ - /// - /// A collection of common random functions which supports various types. - /// - public static class Randomf - { - // Single instance for random - private static Random _rand = new Random(0); - - //TODO: Implement some form of generic method for numerics? - - /// - /// Sets the seed of the random number generator. - /// Constructs a new instance. - /// - /// The new seed. - public static void SetSeed(int newSeed) => _rand = new Random(newSeed); - - /// - /// Generates a random . - /// - /// A thats either true or false. - public static bool RandomBool() => RandomInt() == 1; - - /// - /// Generates a random with a weight value to adjust preference. - /// - /// Normalized value that determines the chance to return true. - /// A thats either true or false. - public static bool RandomBoolWithWeight(float weight = 0) => weight >= RandomFloat(); - - /// - /// Generates a random value between min and max. - /// - /// The lower boundary. - /// The upper boundary. - /// A between min and max. - public static byte RandomByte(byte min = 0, byte max = 1) - { - return (byte)_rand.Next(min, max+1); - } - - /// - /// Generates a random by using a single value as both upper and lower boundary. - /// - /// Defines both upper and lower boundary. - /// A that is between +value and -value. - public static byte RandomUniformByte(byte value = 1) - { - int max = Math.Abs(value); - int min = max * -1; - - return (byte)_rand.Next(min, max); - } - - /// - /// Generates a random value between min and max. - /// - /// The lower boundary. - /// The upper boundary. - /// A random between min and max. - public static int RandomInt(int min = 0, int max = 1) - { - return _rand.Next(min, max+1); - } - - /// - /// Generates a random by using a single value as both upper and lower boundary. - /// - /// Defines both upper and lower boundary. - /// A random between +value and -value. - public static int RandomUniformInt(int value = 1) - { - int max = Math.Abs(value); - int min = max * -1; - - return _rand.Next(min, max); - } - - /// - /// Generates a random value between min and max. - /// - /// The lower boundary. - /// The upper boundary. - /// A random between min and max. - public static float RandomFloat(float min = 0.0f, float max = 1.0f) - { - return (float)_rand.NextDouble() * (max - min) + min; - } - - /// - /// Generates a random by using a single value as both upper and lower boundary. - /// - /// Defines both upper and lower boundary. - /// A random between +value and -value. - public static float RandomUniformFloat(float value = 1.0f) - { - // Ensure that in case of negative values we still return a value between + value and - value. - float max = Math.Abs(value); - float min = max * -1; - - return (float)_rand.NextDouble() * (max - min) + min; - } - - /// - /// Generates a random . - /// - /// A random . - public static Quaternion RandomQuaternion() - { - return Quaternion.Euler( - RandomUniformFloat(180), - RandomUniformFloat(180), - RandomUniformFloat(180)); - } - - /// - /// Generates a uniformly distributed random unit length vector point on a unit sphere. - /// - /// A random . - public static Vector3 RandomVector3() - { - Vector3 output; - - do - { - // Create random float with a mean of 0 and deviation of ±1; - output.X = RandomFloat() * 2.0f - 1.0f; - output.Y = RandomFloat() * 2.0f - 1.0f; - output.Z = RandomFloat() * 2.0f - 1.0f; - - } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); - - output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); - - return output; - } - - /// - /// Generates a uniformly distributed random unit length vector point on a unit sphere in 2D. - /// - /// A random - public static Vector2 RandomVector2() - { - Vector2 output; - - do - { - output.X = RandomFloat() * 2.0f - 1.0f; - output.Y = RandomFloat() * 2.0f - 1.0f; - - } while (output.LengthSquared > 1 || output.LengthSquared< 1e-6f); - - output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); - - return output; - } - - /// - /// Generates a random color. - /// - /// Should the color be generated from a single random Hue value or separate values for each channel. - /// Randomize the alpha value. - /// A nice random color. - public static Color RandomColor(bool trueRandom, bool randomAlpha) - { - float alpha = randomAlpha ? RandomFloat() : 1f; - - if (!trueRandom) - return Color.FromHSV(RandomFloat(0f, 360f), 1f, 1f, alpha); - - return new Color( - RandomFloat(0f, 255f), - RandomFloat(0f, 255f), - RandomFloat(0f, 255f), alpha); - } - } -} diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index e9030af77..ff3439b77 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -60,172 +60,6 @@ namespace FlaxEngine.Utilities }, removeEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None); } - /// - /// Gets a random double. - /// - /// The random. - /// The maximum value - /// A random double - public static double NextDouble(this Random random, double maxValue) - { - return random.NextDouble() * maxValue; - } - - /// - /// Gets a random double. - /// - /// The random. - /// The minimum value - /// The maximum value - /// A random double - public static double NextDouble(this Random random, double minValue, double maxValue) - { - return random.NextDouble() * (maxValue - minValue) + minValue; - } - - /// - /// Gets a random float. - /// - /// The random. - /// A random float - public static float NextFloat(this Random random) - { - return (float)random.NextDouble(); - } - - /// - /// Gets a random float. - /// - /// The random. - /// The maximum value - /// A random float - public static float NextFloat(this Random random, float maxValue) - { - return (float)random.NextDouble() * maxValue; - } - - /// - /// Gets a random float. - /// - /// The random. - /// The minimum value - /// The maximum value - /// - public static float NextFloat(this Random random, float minValue, float maxValue) - { - return (float)random.NextDouble() * (maxValue - minValue) + minValue; - } - - /// - /// Gets a random Color. - /// - /// The random. - /// - public static Color NextColor(this Random random) - { - return new Color(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector2 with components in range [0;1]. - /// - /// The random. - /// - public static Vector2 NextVector2(this Random random) - { - return new Vector2(NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector3 with components in range [0;1]. - /// - /// The random. - /// - public static Vector3 NextVector3(this Random random) - { - return new Vector3(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Vector4 with components in range [0;1]. - /// - /// The random. - /// - public static Vector4 NextVector4(this Random random) - { - return new Vector4(NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f), - NextFloat(random, 1.0f)); - } - - /// - /// Gets a random Quaternion. - /// - /// The random. - /// - public static Quaternion NextQuaternion(this Random random) - { - return Quaternion.Euler(NextFloat(random, -180, 180), - NextFloat(random, -180, 180), - NextFloat(random, -180, 180)); - } - - /// - /// Gets a random 64-bit signed integer value. - /// - /// The random. - /// - internal static long NextLong(this Random random) - { - var numArray = new byte[8]; - random.NextBytes(numArray); - return (long)(BitConverter.ToUInt64(numArray, 0) & 9223372036854775807L); - } - - /// - /// Generates a random normalized 2D direction vector. - /// - /// An instance of . - /// A random normalized 2D direction vector. - public static Vector2 NextDirection2D(this Random random) - { - return Vector2.Normalize(new Vector2((float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random normalized 3D direction vector. - /// - /// An instance of . - /// A random normalized 3D direction vector. - public static Vector3 NextDirection3D(this Random random) - { - return Vector3.Normalize(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random point in a circle of a given radius. - /// - /// An instance of . - /// Radius of circle. Default 1.0f. - /// A random point in a circle of a given radius. - public static Vector2 PointInACircle(this Random random, float radius = 1.0f) - { - var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 - { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, - }; - } - /// /// Adds the elements of the specified collection to the end of the . /// @@ -404,5 +238,194 @@ namespace FlaxEngine.Utilities collection[n] = value; } } + + /// + /// Generates a random . + /// + /// An instance of . + /// A thats either true or false. + public static bool NextBool(this Random random) => random.Next(2) == 1; + + /// + /// Generates a random with a weight value to adjust preference. + /// + /// An instance of . + /// Normalized value that determines the chance to return true. + /// A thats either true or false. + public static bool NextBool(this Random random, float weight = 0.5f) => weight >= NextFloat(random); + + /// + /// Generates a random value between min and max. + /// + /// An instance of . + /// The lower boundary. + /// The upper boundary. + /// A between min and max. + public static byte NextByte(this Random random, byte min = 0, byte max = 2) + { + return (byte)random.Next(min, max); + } + + /// + /// Generates a random value between min and max. + /// + /// An instance of . + /// The min value. + /// The max value. + /// A random between min and max. + public static float NextFloat(this Random random, float min = 0.0f, float max = 2.0f) + { + return (float)random.NextDouble() * (max - min) + min; + } + + /// + /// Generates a random . + /// + /// An instance of . + /// Should the roll value be randomized. + /// A random . + public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) + { + return Quaternion.Euler( + NextFloat(random, -180, 180), + NextFloat(random, -180, 180), + randomRoll ? NextFloat(random, -180, 180) : 0); + } + + /// + /// Generates a uniformly distributed random unit length vector point on a unit sphere. + /// + /// An instance of . + /// A random . + public static Vector3 NextVector3(this Random random) + { + Vector3 output; + + do + { + // Create random float with a mean of 0 and deviation of ±1; + output.X = NextFloat(random) * 2.0f - 1.0f; + output.Y = NextFloat(random) * 2.0f - 1.0f; + output.Z = NextFloat(random) * 2.0f - 1.0f; + + } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); + + output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); + + return output; + } + + /// + /// Generates a uniformly distributed random unit length vector point on a unit sphere in 2D. + /// + /// An instance of . + /// A random . + public static Vector2 NextVector2(this Random random) + { + Vector2 output; + + do + { + output.X = NextFloat(random) * 2.0f - 1.0f; + output.Y = NextFloat(random) * 2.0f - 1.0f; + + } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); + + output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); + + return output; + } + + /// + /// Generates a random . + /// + /// An instance of . + /// Should the color be generated from a single random Hue value or separate values for each channel. + /// Randomize the alpha value. + /// A nice random . + public static Color NextColor(this Random random, bool trueRandom, bool randomAlpha) + { + float alpha = randomAlpha ? NextFloat(random) : 1f; + + if (!trueRandom) + return Color.FromHSV(NextFloat(random, 0f, 360f), 1f, 1f, alpha); + + return new Color( + NextFloat(random, 0f, 255f), + NextFloat(random, 0f, 255f), + NextFloat(random, 0f, 255f), alpha); + } + + /// + /// Gets a random . + /// + /// An instance of . + /// The maximum value + /// A random + public static double NextDouble(this Random random, double maxValue = 2.0d) + { + return random.NextDouble() * maxValue; + } + + /// + /// Gets a random . + /// + /// An instance of . + /// The minimum value + /// The maximum value + /// A random + public static double NextDouble(this Random random, double minValue = 0.0d, double maxValue = 2.0d) + { + return random.NextDouble() * (maxValue - minValue) + minValue; + } + + /// + /// Gets a random . + /// + /// An instance of . + /// A random + internal static long NextLong(this Random random) + { + var numArray = new byte[8]; + random.NextBytes(numArray); + return (long)(BitConverter.ToUInt64(numArray, 0) & 9223372036854775807L); + } + + /// + /// Generates a random normalized 2D direction . + /// + /// An instance of . + /// A random normalized 2D direction . + public static Vector2 NextDirection2D(this Random random) + { + return Vector2.Normalize(new Vector2((float)random.NextDouble(), (float)random.NextDouble())); + } + + /// + /// Generates a random normalized 3D direction . + /// + /// An instance of . + /// A random normalized 3D direction . + public static Vector3 NextDirection3D(this Random random) + { + return Vector3.Normalize(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())); + } + + /// + /// Generates a random point in a circle of a given radius. + /// + /// An instance of . + /// Radius of circle. Default 1.0f. + /// A random point in a circle of a given radius. + public static Vector2 PointInACircle(this Random random, float radius = 1.0f) + { + var randomRadius = (float)random.NextDouble() * radius; + + return new Vector2 + { + X = (float)Math.Cos(random.NextDouble()) * randomRadius, + Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + }; + } } } From 6649f0d4c23dc680226cd270158ab43b1ab197a3 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 13 Jan 2021 21:58:16 +0100 Subject: [PATCH 04/13] Removed unnecessary overload Same can be achieved by specifiying max. --- Source/Engine/Utilities/Extensions.cs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index ff3439b77..15238a701 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -286,6 +286,7 @@ namespace FlaxEngine.Utilities /// A random . public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) { + return Quaternion.Euler( NextFloat(random, -180, 180), NextFloat(random, -180, 180), @@ -360,23 +361,12 @@ namespace FlaxEngine.Utilities /// Gets a random . /// /// An instance of . - /// The maximum value + /// The minimum value + /// The maximum value /// A random - public static double NextDouble(this Random random, double maxValue = 2.0d) + public static double NextDouble(this Random random, double min = 0.0d, double max = 2.0d) { - return random.NextDouble() * maxValue; - } - - /// - /// Gets a random . - /// - /// An instance of . - /// The minimum value - /// The maximum value - /// A random - public static double NextDouble(this Random random, double minValue = 0.0d, double maxValue = 2.0d) - { - return random.NextDouble() * (maxValue - minValue) + minValue; + return random.NextDouble() * (max - min) + min; } /// From 31dd5b852e1cd15dd7f67c141371adac32b2301f Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 13 Jan 2021 22:04:16 +0100 Subject: [PATCH 05/13] Update Extensions.cs --- Source/Engine/Utilities/Extensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index 15238a701..1e22df1c4 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -258,8 +258,8 @@ namespace FlaxEngine.Utilities /// Generates a random value between min and max. /// /// An instance of . - /// The lower boundary. - /// The upper boundary. + /// The minimum value. + /// The maximum value. /// A between min and max. public static byte NextByte(this Random random, byte min = 0, byte max = 2) { @@ -270,8 +270,8 @@ namespace FlaxEngine.Utilities /// Generates a random value between min and max. /// /// An instance of . - /// The min value. - /// The max value. + /// The minimum value. + /// The maximum value. /// A random between min and max. public static float NextFloat(this Random random, float min = 0.0f, float max = 2.0f) { From 75c3388c00ff4d6e3ee93503b68ece30b9cce159 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Thu, 14 Jan 2021 01:12:13 +0100 Subject: [PATCH 06/13] Corrected comments --- Source/Engine/Utilities/Extensions.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index 1e22df1c4..a47a38ecb 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -304,7 +304,7 @@ namespace FlaxEngine.Utilities do { - // Create random float with a mean of 0 and deviation of ±1; + // Create random float with a mean of 0 and deviation of ±1 output.X = NextFloat(random) * 2.0f - 1.0f; output.Y = NextFloat(random) * 2.0f - 1.0f; output.Z = NextFloat(random) * 2.0f - 1.0f; @@ -361,9 +361,9 @@ namespace FlaxEngine.Utilities /// Gets a random . /// /// An instance of . - /// The minimum value - /// The maximum value - /// A random + /// The minimum value. + /// The maximum value. + /// A random . public static double NextDouble(this Random random, double min = 0.0d, double max = 2.0d) { return random.NextDouble() * (max - min) + min; @@ -373,7 +373,7 @@ namespace FlaxEngine.Utilities /// Gets a random . /// /// An instance of . - /// A random + /// A random . internal static long NextLong(this Random random) { var numArray = new byte[8]; From 437018e614aaf1eea97058aaaf37ced13f5b4d7b Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Thu, 14 Jan 2021 11:50:03 +0100 Subject: [PATCH 07/13] Applied review changes Applied almost all review changes. --- Source/Engine/Utilities/Extensions.cs | 118 ++++++++++---------------- 1 file changed, 47 insertions(+), 71 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index a47a38ecb..f6d2be4c5 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -243,16 +243,9 @@ namespace FlaxEngine.Utilities /// Generates a random . /// /// An instance of . - /// A thats either true or false. - public static bool NextBool(this Random random) => random.Next(2) == 1; - - /// - /// Generates a random with a weight value to adjust preference. - /// - /// An instance of . /// Normalized value that determines the chance to return true. /// A thats either true or false. - public static bool NextBool(this Random random, float weight = 0.5f) => weight >= NextFloat(random); + public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight; /// /// Generates a random value between min and max. @@ -260,24 +253,35 @@ namespace FlaxEngine.Utilities /// An instance of . /// The minimum value. /// The maximum value. - /// A between min and max. - public static byte NextByte(this Random random, byte min = 0, byte max = 2) + /// A random between min and max. + public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue) { return (byte)random.Next(min, max); } /// - /// Generates a random value between min and max. + /// Generates a random value between min and max. /// /// An instance of . /// The minimum value. /// The maximum value. /// A random between min and max. - public static float NextFloat(this Random random, float min = 0.0f, float max = 2.0f) + public static float NextFloat(this Random random, float min = 0.0f, float max = 1.0f) { return (float)random.NextDouble() * (max - min) + min; } + /// + /// Generates a random value between 0 and max. + /// + /// An instance of . + /// The maximum value. + /// A random between min and max. + public static float NextFloat(this Random random, float max) + { + return (float)random.NextDouble() * max; + } + /// /// Generates a random . /// @@ -286,7 +290,6 @@ namespace FlaxEngine.Utilities /// A random . public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) { - return Quaternion.Euler( NextFloat(random, -180, 180), NextFloat(random, -180, 180), @@ -301,6 +304,7 @@ namespace FlaxEngine.Utilities public static Vector3 NextVector3(this Random random) { Vector3 output; + float l; do { @@ -309,42 +313,40 @@ namespace FlaxEngine.Utilities output.Y = NextFloat(random) * 2.0f - 1.0f; output.Z = NextFloat(random) * 2.0f - 1.0f; - } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); + l = output.LengthSquared; - output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); + } while (l > 1 || l < Mathf.Epsilon); + + output.Normalize(); return output; } /// - /// Generates a uniformly distributed random unit length vector point on a unit sphere in 2D. + /// Generates a random point in a circle of a given radius. /// /// An instance of . + /// Radius of circle. Default 1.0f./>. /// A random . - public static Vector2 NextVector2(this Random random) + public static Vector2 NextVector2(this Random random, float radius = 1.0f) { - Vector2 output; + var randomRadius = (float)random.NextDouble() * radius; - do + return new Vector2 { - output.X = NextFloat(random) * 2.0f - 1.0f; - output.Y = NextFloat(random) * 2.0f - 1.0f; - - } while (output.LengthSquared > 1 || output.LengthSquared < 1e-6f); - - output *= (1.0f / (float)Math.Sqrt(output.LengthSquared)); - - return output; + X = (float)Math.Cos(random.NextDouble()) * randomRadius, + Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + }; } /// /// Generates a random . /// /// An instance of . - /// Should the color be generated from a single random Hue value or separate values for each channel. + /// Should the color be generated from a single random hue value or separate values for each channel. /// Randomize the alpha value. /// A nice random . - public static Color NextColor(this Random random, bool trueRandom, bool randomAlpha) + public static Color NextColor(this Random random, bool trueRandom = false, bool randomAlpha = false) { float alpha = randomAlpha ? NextFloat(random) : 1f; @@ -352,9 +354,9 @@ namespace FlaxEngine.Utilities return Color.FromHSV(NextFloat(random, 0f, 360f), 1f, 1f, alpha); return new Color( - NextFloat(random, 0f, 255f), - NextFloat(random, 0f, 255f), - NextFloat(random, 0f, 255f), alpha); + NextFloat(random), + NextFloat(random), + NextFloat(random), alpha); } /// @@ -364,11 +366,22 @@ namespace FlaxEngine.Utilities /// The minimum value. /// The maximum value. /// A random . - public static double NextDouble(this Random random, double min = 0.0d, double max = 2.0d) + public static double NextDouble(this Random random, double min = 0.0d, double max = 1.0d) { return random.NextDouble() * (max - min) + min; } + /// + /// Gets a random . + /// + /// An instance of . + /// The maximum value. + /// A random . + public static double NextDouble(this Random random, double max = 1.0d) + { + return random.NextDouble() * max; + } + /// /// Gets a random . /// @@ -378,44 +391,7 @@ namespace FlaxEngine.Utilities { var numArray = new byte[8]; random.NextBytes(numArray); - return (long)(BitConverter.ToUInt64(numArray, 0) & 9223372036854775807L); - } - - /// - /// Generates a random normalized 2D direction . - /// - /// An instance of . - /// A random normalized 2D direction . - public static Vector2 NextDirection2D(this Random random) - { - return Vector2.Normalize(new Vector2((float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random normalized 3D direction . - /// - /// An instance of . - /// A random normalized 3D direction . - public static Vector3 NextDirection3D(this Random random) - { - return Vector3.Normalize(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())); - } - - /// - /// Generates a random point in a circle of a given radius. - /// - /// An instance of . - /// Radius of circle. Default 1.0f. - /// A random point in a circle of a given radius. - public static Vector2 PointInACircle(this Random random, float radius = 1.0f) - { - var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 - { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, - }; + return (long)(BitConverter.ToUInt64(numArray, 0) & long.MaxValue); } } } From 8e44e6b23cf2494959c6d278f8bf73f1cc0ea4c9 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Thu, 14 Jan 2021 12:28:51 +0100 Subject: [PATCH 08/13] Applied additional review changes Moved out the random Color created via HSV to its own function as @stefnotch suggested. Added + 1 to the max value in NextByte to include max as a possible value. --- Source/Engine/Utilities/Extensions.cs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index f6d2be4c5..d7329c8bd 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -256,7 +256,7 @@ namespace FlaxEngine.Utilities /// A random between min and max. public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue) { - return (byte)random.Next(min, max); + return (byte)random.Next(min, max+1); } /// @@ -343,20 +343,25 @@ namespace FlaxEngine.Utilities /// Generates a random . /// /// An instance of . - /// Should the color be generated from a single random hue value or separate values for each channel. /// Randomize the alpha value. /// A nice random . - public static Color NextColor(this Random random, bool trueRandom = false, bool randomAlpha = false) + public static Color NextColor(this Random random, bool randomAlpha = false) { - float alpha = randomAlpha ? NextFloat(random) : 1f; - - if (!trueRandom) - return Color.FromHSV(NextFloat(random, 0f, 360f), 1f, 1f, alpha); - return new Color( NextFloat(random), NextFloat(random), - NextFloat(random), alpha); + NextFloat(random), randomAlpha ? NextFloat(random) : 1f); + } + + /// + /// Generates a random using a single random value for hue. + /// + /// An instance of . + /// Randomize the alpha value. + /// A nice random . + public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false) + { + return new ColorHSV(NextFloat(random, 0f, 360f), 1f, 1f, randomAlpha ? NextFloat(random) : 1f); } /// From 00c40e279cc7bb9c21fbde1357ac933477fe34f1 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Thu, 14 Jan 2021 13:56:10 +0100 Subject: [PATCH 09/13] Update Extensions.cs --- Source/Engine/Utilities/Extensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index d7329c8bd..339c1c368 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -256,7 +256,7 @@ namespace FlaxEngine.Utilities /// A random between min and max. public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue) { - return (byte)random.Next(min, max+1); + return (byte)random.Next(min, max == byte.MaxValue ? byte.MaxValue + 1 : max); } /// @@ -354,7 +354,7 @@ namespace FlaxEngine.Utilities } /// - /// Generates a random using a single random value for hue. + /// Generates a random . /// /// An instance of . /// Randomize the alpha value. From 042694f45e8c2aa3c7df6d64d0bd0d4122a687e7 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Tue, 9 Feb 2021 17:11:44 +0100 Subject: [PATCH 10/13] Added random enum --- Source/Engine/Utilities/Extensions.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index 339c1c368..29aa7329a 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -398,5 +398,17 @@ namespace FlaxEngine.Utilities random.NextBytes(numArray); return (long)(BitConverter.ToUInt64(numArray, 0) & long.MaxValue); } + + /// + /// Returns a random value of the given enum. + /// + /// The enum to get the value from. + /// An instance of . + /// A random enum value. + public static TEnum NextEnum(this Random random) + { + Array values = Enum.GetValues(typeof(TEnum)); + return (TEnum)values.GetValue(random.Next(values.Length)); + } } } From 654285e57f7b5a3345480c2d2e271b04bfed3cb2 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 17 Feb 2021 00:30:24 +0100 Subject: [PATCH 11/13] Separated NextVector and NextUnitVector, refactoring While I was at it I made some slight refactoring making it much more readable and establish consistency --- Source/Engine/Utilities/Extensions.cs | 94 ++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index 29aa7329a..d5e383ab6 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -291,9 +291,26 @@ namespace FlaxEngine.Utilities public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) { return Quaternion.Euler( - NextFloat(random, -180, 180), - NextFloat(random, -180, 180), - randomRoll ? NextFloat(random, -180, 180) : 0); + NextFloat(random, -180.0f, 180.0f), + NextFloat(random, -180.0f, 180.0f), + randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f); + } + + /// + /// Generates a random point in a circle of a given radius. + /// + /// An instance of . + /// Radius of circle. Default 1.0f./>. + /// A random . + public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f) + { + var randomRadius = (float)random.NextDouble() * radius; + + return new Vector2 + { + X = (float)Math.Cos(random.NextDouble()) * randomRadius, + Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + }; } /// @@ -301,7 +318,7 @@ namespace FlaxEngine.Utilities /// /// An instance of . /// A random . - public static Vector3 NextVector3(this Random random) + public static Vector3 NextUnitVector3(this Random random) { Vector3 output; float l; @@ -323,19 +340,53 @@ namespace FlaxEngine.Utilities } /// - /// Generates a random point in a circle of a given radius. + /// Gets a random with components in a given range. /// /// An instance of . - /// Radius of circle. Default 1.0f./>. + /// The minimum value. + /// The maximum value. /// A random . - public static Vector2 NextVector2(this Random random, float radius = 1.0f) + public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f) { - var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 + return new Vector2 { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max) + }; + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector3 + { + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max), + Z = NextFloat(random, min, max) + }; + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector4 + { + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max), + Z = NextFloat(random, min, max), + W = NextFloat(random, min, max) }; } @@ -347,10 +398,13 @@ namespace FlaxEngine.Utilities /// A nice random . public static Color NextColor(this Random random, bool randomAlpha = false) { - return new Color( - NextFloat(random), - NextFloat(random), - NextFloat(random), randomAlpha ? NextFloat(random) : 1f); + return new Color + { + R = NextFloat(random), + G = NextFloat(random), + B = NextFloat(random), + A = randomAlpha ? NextFloat(random) : 1.0f + }; } /// @@ -361,7 +415,13 @@ namespace FlaxEngine.Utilities /// A nice random . public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false) { - return new ColorHSV(NextFloat(random, 0f, 360f), 1f, 1f, randomAlpha ? NextFloat(random) : 1f); + return new ColorHSV + { + H = NextFloat(random, 0.0f, 360.0f), + S = 1.0f, + V = 1.0f, + A = randomAlpha ? NextFloat(random) : 1.0f + }; } /// From 1c1e9bb1053c994afd0a513e67b2b9b6beb6a95c Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Perrier Date: Wed, 17 Feb 2021 12:44:44 +0100 Subject: [PATCH 12/13] Fix re-drawing the background when alpha > 0 . --- Source/Engine/UI/GUI/Tooltip.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Engine/UI/GUI/Tooltip.cs b/Source/Engine/UI/GUI/Tooltip.cs index 24c0651ed..b4abfaf66 100644 --- a/Source/Engine/UI/GUI/Tooltip.cs +++ b/Source/Engine/UI/GUI/Tooltip.cs @@ -223,8 +223,6 @@ namespace FlaxEngine.GUI TextAlignment.Center, TextWrapping.WrapWords ); - - base.Draw(); } /// From 4aabb81c1f05d2a6de0ebe5a4e58e677a6f9f258 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 18 Feb 2021 22:43:46 +0100 Subject: [PATCH 13/13] Use constructors for random generation methods --- Source/Engine/Utilities/Extensions.cs | 86 +++------------------------ 1 file changed, 7 insertions(+), 79 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index d5e383ab6..b3f6d2053 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -70,15 +70,9 @@ namespace FlaxEngine.Utilities public static void AddRange(this ICollection destination, IEnumerable collection) { if (destination == null) - { throw new ArgumentNullException(nameof(destination)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { destination.Add(item); @@ -95,15 +89,9 @@ namespace FlaxEngine.Utilities public static void EnqueueRange(this Queue queue, IEnumerable collection) { if (queue == null) - { throw new ArgumentNullException(nameof(queue)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { queue.Enqueue(item); @@ -120,15 +108,9 @@ namespace FlaxEngine.Utilities public static void PushRange(this Stack stack, IEnumerable collection) { if (stack == null) - { throw new ArgumentNullException(nameof(stack)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - foreach (var item in collection) { stack.Push(item); @@ -145,15 +127,9 @@ namespace FlaxEngine.Utilities public static void ForEach(this IEnumerable source, Action action) { if (source == null) - { throw new ArgumentNullException(nameof(source)); - } - if (action == null) - { throw new ArgumentNullException(nameof(action)); - } - foreach (var item in source) { action(item); @@ -172,15 +148,9 @@ namespace FlaxEngine.Utilities public static T Choose(this Random random, IList collection) { if (random == null) - { throw new ArgumentNullException(nameof(random)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - return collection[random.Next(collection.Count)]; } @@ -196,15 +166,9 @@ namespace FlaxEngine.Utilities public static T Choose(this Random random, params T[] collection) { if (random == null) - { throw new ArgumentNullException(nameof(random)); - } - if (collection == null) - { throw new ArgumentNullException(nameof(collection)); - } - return collection[random.Next(collection.Length)]; } @@ -290,10 +254,7 @@ namespace FlaxEngine.Utilities /// A random . public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) { - return Quaternion.Euler( - NextFloat(random, -180.0f, 180.0f), - NextFloat(random, -180.0f, 180.0f), - randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f); + return Quaternion.Euler(NextFloat(random, -180.0f, 180.0f), NextFloat(random, -180.0f, 180.0f), randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f); } /// @@ -305,12 +266,7 @@ namespace FlaxEngine.Utilities public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f) { var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 - { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, - }; + return new Vector2((float)Math.Cos(random.NextDouble()) * randomRadius, (float)Math.Sin(random.NextDouble()) * randomRadius); } /// @@ -331,7 +287,6 @@ namespace FlaxEngine.Utilities output.Z = NextFloat(random) * 2.0f - 1.0f; l = output.LengthSquared; - } while (l > 1 || l < Mathf.Epsilon); output.Normalize(); @@ -348,11 +303,7 @@ namespace FlaxEngine.Utilities /// A random . public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f) { - return new Vector2 - { - X = NextFloat(random, min, max), - Y = NextFloat(random, min, max) - }; + return new Vector2(NextFloat(random, min, max), NextFloat(random, min, max)); } /// @@ -364,12 +315,7 @@ namespace FlaxEngine.Utilities /// A random . public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f) { - return new Vector3 - { - X = NextFloat(random, min, max), - Y = NextFloat(random, min, max), - Z = NextFloat(random, min, max) - }; + return new Vector3(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max)); } /// @@ -381,13 +327,7 @@ namespace FlaxEngine.Utilities /// A random . public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f) { - return new Vector4 - { - X = NextFloat(random, min, max), - Y = NextFloat(random, min, max), - Z = NextFloat(random, min, max), - W = NextFloat(random, min, max) - }; + return new Vector4(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max)); } /// @@ -398,13 +338,7 @@ namespace FlaxEngine.Utilities /// A nice random . public static Color NextColor(this Random random, bool randomAlpha = false) { - return new Color - { - R = NextFloat(random), - G = NextFloat(random), - B = NextFloat(random), - A = randomAlpha ? NextFloat(random) : 1.0f - }; + return new Color(NextFloat(random), NextFloat(random), NextFloat(random), randomAlpha ? NextFloat(random) : 1.0f); } /// @@ -415,13 +349,7 @@ namespace FlaxEngine.Utilities /// A nice random . public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false) { - return new ColorHSV - { - H = NextFloat(random, 0.0f, 360.0f), - S = 1.0f, - V = 1.0f, - A = randomAlpha ? NextFloat(random) : 1.0f - }; + return new ColorHSV(NextFloat(random, 0.0f, 360.0f), 1.0f, 1.0f, randomAlpha ? NextFloat(random) : 1.0f); } ///