Repository: FlaxEngine/FlaxEngine
File: Source/Engine/Utilities/Extensions.cs (C#)

Code before:
        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);
        }

        /// <summary>
        /// Generates a random <see cref="Vector2"/> point in a circle of a given radius.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="radius">Radius of circle. Default 1.0f./>.</param>
        /// <returns>A random <see cref="Vector2"/>.</returns>
        public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f)
        {
            var randomRadius = (float)random.NextDouble() * radius;
            return new Vector2((float)Math.Cos(random.NextDouble()) * randomRadius, (float)Math.Sin(random.NextDouble()) * randomRadius);
        }
        
        /// <summary>
        /// Generates a random <see cref="Vector2"/> point on a circle of a given radius.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="radius">Radius of circle. Default 1.0f./>.</param>
        /// <returns>A random <see cref="Vector2"/>.</returns>
        public static Vector2 NextUnitCircleVector2(this Random random, float radius = 1.0f)
        {
            double randomDegree = random.NextDouble() * 360;
            return new Vector2((float)Math.Cos(randomDegree) * radius, (float)Math.Sin(randomDegree) * radius);
        }

        /// <summary>
        /// Generates a uniformly distributed random unit length vector point on a unit sphere.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <returns>A random <see cref="Vector3"/>.</returns>
        public static Vector3 NextUnitVector3(this Random random)
        {
            Vector3 output;
            Real l;

            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;

                l = output.LengthSquared;
            } while (l > 1 || l < Mathf.Epsilon);

            output.Normalize();


Review comment (suggestion):
I think this should be: `double randomDegree = random.NextDouble() * Mathf.RevolutionsToRadians;` so cos/sin will get proper value in radians.

Code after:
        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);
        }

        /// <summary>
        /// Generates a random <see cref="Vector2"/> point in a circle of a given radius.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="radius">Radius of circle. Default 1.0f./>.</param>
        /// <returns>A random <see cref="Vector2"/>.</returns>
        public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f)
        {
            var randomRadius = (float)random.NextDouble() * radius;
            return new Vector2((float)Math.Cos(random.NextDouble()) * randomRadius, (float)Math.Sin(random.NextDouble()) * randomRadius);
        }
        
        /// <summary>
        /// Generates a random <see cref="Vector2"/> point on a circle of a given radius.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="radius">Radius of circle. Default 1.0f./>.</param>
        /// <returns>A random <see cref="Vector2"/>.</returns>
        public static Vector2 NextUnitCircleVector2(this Random random, float radius = 1.0f)
        {
            double randomRadian = random.NextDouble() * Mathf.RevolutionsToRadians;
            return new Vector2((float)Math.Cos(randomRadian) * radius, (float)Math.Sin(randomRadian) * radius);
        }

        /// <summary>
        /// Generates a uniformly distributed random unit length vector point on a unit sphere.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <returns>A random <see cref="Vector3"/>.</returns>
        public static Vector3 NextUnitVector3(this Random random)
        {
            Vector3 output;
            Real l;

            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;

                l = output.LengthSquared;
            } while (l > 1 || l < Mathf.Epsilon);

            output.Normalize();