Use constructors for random generation methods
This commit is contained in:
@@ -70,15 +70,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> collection)
|
public static void AddRange<T>(this ICollection<T> destination, IEnumerable<T> collection)
|
||||||
{
|
{
|
||||||
if (destination == null)
|
if (destination == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(destination));
|
throw new ArgumentNullException(nameof(destination));
|
||||||
}
|
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(collection));
|
throw new ArgumentNullException(nameof(collection));
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in collection)
|
foreach (var item in collection)
|
||||||
{
|
{
|
||||||
destination.Add(item);
|
destination.Add(item);
|
||||||
@@ -95,15 +89,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static void EnqueueRange<T>(this Queue<T> queue, IEnumerable<T> collection)
|
public static void EnqueueRange<T>(this Queue<T> queue, IEnumerable<T> collection)
|
||||||
{
|
{
|
||||||
if (queue == null)
|
if (queue == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(queue));
|
throw new ArgumentNullException(nameof(queue));
|
||||||
}
|
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(collection));
|
throw new ArgumentNullException(nameof(collection));
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in collection)
|
foreach (var item in collection)
|
||||||
{
|
{
|
||||||
queue.Enqueue(item);
|
queue.Enqueue(item);
|
||||||
@@ -120,15 +108,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static void PushRange<T>(this Stack<T> stack, IEnumerable<T> collection)
|
public static void PushRange<T>(this Stack<T> stack, IEnumerable<T> collection)
|
||||||
{
|
{
|
||||||
if (stack == null)
|
if (stack == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(stack));
|
throw new ArgumentNullException(nameof(stack));
|
||||||
}
|
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(collection));
|
throw new ArgumentNullException(nameof(collection));
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in collection)
|
foreach (var item in collection)
|
||||||
{
|
{
|
||||||
stack.Push(item);
|
stack.Push(item);
|
||||||
@@ -145,15 +127,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
|
||||||
{
|
{
|
||||||
if (source == null)
|
if (source == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(source));
|
throw new ArgumentNullException(nameof(source));
|
||||||
}
|
|
||||||
|
|
||||||
if (action == null)
|
if (action == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(action));
|
throw new ArgumentNullException(nameof(action));
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in source)
|
foreach (var item in source)
|
||||||
{
|
{
|
||||||
action(item);
|
action(item);
|
||||||
@@ -172,15 +148,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static T Choose<T>(this Random random, IList<T> collection)
|
public static T Choose<T>(this Random random, IList<T> collection)
|
||||||
{
|
{
|
||||||
if (random == null)
|
if (random == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(random));
|
throw new ArgumentNullException(nameof(random));
|
||||||
}
|
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(collection));
|
throw new ArgumentNullException(nameof(collection));
|
||||||
}
|
|
||||||
|
|
||||||
return collection[random.Next(collection.Count)];
|
return collection[random.Next(collection.Count)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,15 +166,9 @@ namespace FlaxEngine.Utilities
|
|||||||
public static T Choose<T>(this Random random, params T[] collection)
|
public static T Choose<T>(this Random random, params T[] collection)
|
||||||
{
|
{
|
||||||
if (random == null)
|
if (random == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(random));
|
throw new ArgumentNullException(nameof(random));
|
||||||
}
|
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(collection));
|
throw new ArgumentNullException(nameof(collection));
|
||||||
}
|
|
||||||
|
|
||||||
return collection[random.Next(collection.Length)];
|
return collection[random.Next(collection.Length)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,10 +254,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A random <see cref="Quaternion"/>.</returns>
|
/// <returns>A random <see cref="Quaternion"/>.</returns>
|
||||||
public static Quaternion NextQuaternion(this Random random, bool randomRoll = false)
|
public static Quaternion NextQuaternion(this Random random, bool randomRoll = false)
|
||||||
{
|
{
|
||||||
return Quaternion.Euler(
|
return Quaternion.Euler(NextFloat(random, -180.0f, 180.0f), NextFloat(random, -180.0f, 180.0f), randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f);
|
||||||
NextFloat(random, -180.0f, 180.0f),
|
|
||||||
NextFloat(random, -180.0f, 180.0f),
|
|
||||||
randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -305,12 +266,7 @@ namespace FlaxEngine.Utilities
|
|||||||
public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f)
|
public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f)
|
||||||
{
|
{
|
||||||
var randomRadius = (float)random.NextDouble() * radius;
|
var randomRadius = (float)random.NextDouble() * radius;
|
||||||
|
return new Vector2((float)Math.Cos(random.NextDouble()) * randomRadius, (float)Math.Sin(random.NextDouble()) * randomRadius);
|
||||||
return new Vector2
|
|
||||||
{
|
|
||||||
X = (float)Math.Cos(random.NextDouble()) * randomRadius,
|
|
||||||
Y = (float)Math.Sin(random.NextDouble()) * randomRadius,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -331,7 +287,6 @@ namespace FlaxEngine.Utilities
|
|||||||
output.Z = NextFloat(random) * 2.0f - 1.0f;
|
output.Z = NextFloat(random) * 2.0f - 1.0f;
|
||||||
|
|
||||||
l = output.LengthSquared;
|
l = output.LengthSquared;
|
||||||
|
|
||||||
} while (l > 1 || l < Mathf.Epsilon);
|
} while (l > 1 || l < Mathf.Epsilon);
|
||||||
|
|
||||||
output.Normalize();
|
output.Normalize();
|
||||||
@@ -348,11 +303,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A random <see cref="Vector2"/>.</returns>
|
/// <returns>A random <see cref="Vector2"/>.</returns>
|
||||||
public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f)
|
public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f)
|
||||||
{
|
{
|
||||||
return new Vector2
|
return new Vector2(NextFloat(random, min, max), NextFloat(random, min, max));
|
||||||
{
|
|
||||||
X = NextFloat(random, min, max),
|
|
||||||
Y = NextFloat(random, min, max)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -364,12 +315,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A random <see cref="Vector3"/>.</returns>
|
/// <returns>A random <see cref="Vector3"/>.</returns>
|
||||||
public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f)
|
public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f)
|
||||||
{
|
{
|
||||||
return new Vector3
|
return new Vector3(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max));
|
||||||
{
|
|
||||||
X = NextFloat(random, min, max),
|
|
||||||
Y = NextFloat(random, min, max),
|
|
||||||
Z = NextFloat(random, min, max)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -381,13 +327,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A random <see cref="Vector4"/>.</returns>
|
/// <returns>A random <see cref="Vector4"/>.</returns>
|
||||||
public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f)
|
public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f)
|
||||||
{
|
{
|
||||||
return new Vector4
|
return new Vector4(NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max), NextFloat(random, min, max));
|
||||||
{
|
|
||||||
X = NextFloat(random, min, max),
|
|
||||||
Y = NextFloat(random, min, max),
|
|
||||||
Z = NextFloat(random, min, max),
|
|
||||||
W = NextFloat(random, min, max)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -398,13 +338,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A nice random <see cref="Color"/>.</returns>
|
/// <returns>A nice random <see cref="Color"/>.</returns>
|
||||||
public static Color NextColor(this Random random, bool randomAlpha = false)
|
public static Color NextColor(this Random random, bool randomAlpha = false)
|
||||||
{
|
{
|
||||||
return new Color
|
return new Color(NextFloat(random), NextFloat(random), NextFloat(random), randomAlpha ? NextFloat(random) : 1.0f);
|
||||||
{
|
|
||||||
R = NextFloat(random),
|
|
||||||
G = NextFloat(random),
|
|
||||||
B = NextFloat(random),
|
|
||||||
A = randomAlpha ? NextFloat(random) : 1.0f
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -415,13 +349,7 @@ namespace FlaxEngine.Utilities
|
|||||||
/// <returns>A nice random <see cref="ColorHSV"/>.</returns>
|
/// <returns>A nice random <see cref="ColorHSV"/>.</returns>
|
||||||
public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false)
|
public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false)
|
||||||
{
|
{
|
||||||
return new ColorHSV
|
return new ColorHSV(NextFloat(random, 0.0f, 360.0f), 1.0f, 1.0f, randomAlpha ? NextFloat(random) : 1.0f);
|
||||||
{
|
|
||||||
H = NextFloat(random, 0.0f, 360.0f),
|
|
||||||
S = 1.0f,
|
|
||||||
V = 1.0f,
|
|
||||||
A = randomAlpha ? NextFloat(random) : 1.0f
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user