From 604c583c36c3da255c28da2dd5d797d64c1b5202 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 13 Jan 2021 21:16:17 +0100 Subject: [PATCH] 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, + }; + } } }