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);
}
}
}