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

Code before:
            {
                throw new ArgumentNullException(nameof(random));
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            int n = collection.Count;
            while (n > 1)
            {
                n--;
                int k = random.Next(n + 1);
                T value = collection[k];
                collection[k] = collection[n];
                collection[n] = value;
            }
        }

        /// <summary>
        /// Generates a random <see cref="bool"/>.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <returns>A <see cref="bool"/> thats either true or false.</returns>
        public static bool NextBool(this Random random) => random.Next(2) == 1;

        /// <summary>
        /// Generates a random <see cref="bool"/> with a weight value to adjust preference.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="weight">Normalized value that determines the chance to return true.</param>
        /// <returns>A <see cref="bool"/> thats either true or false.</returns>
        public static bool NextBool(this Random random, float weight = 0.5f) => weight >= NextFloat(random);

        /// <summary>
        /// Generates a random <see cref="byte"/> value between min and max.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>A <see cref="byte"/> between min and max.</returns>
        public static byte NextByte(this Random random, byte min = 0, byte max = 2)
        {
            return (byte)random.Next(min, max);
        }

        /// <summary>
        /// Generates a random <see cref="float"/>  value between min and max.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>

Review comment (bug):
I do realize that this is a mostly silly microoptimisation, but technically, [`random.NextDouble() < 0.5`](https://stackoverflow.com/a/19191165) would be faster.

The upside of changing it to that is that it would let you merge this method with the next one.

Code after:
            {
                throw new ArgumentNullException(nameof(random));
            }

            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            int n = collection.Count;
            while (n > 1)
            {
                n--;
                int k = random.Next(n + 1);
                T value = collection[k];
                collection[k] = collection[n];
                collection[n] = value;
            }
        }

        /// <summary>
        /// Generates a random <see cref="bool"/>.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="weight">Normalized value that determines the chance to return true.</param>
        /// <returns>A <see cref="bool"/> thats either true or false.</returns>
        public static bool NextBool(this Random random, float weight = 0.5f) => random.NextDouble() < weight;

        /// <summary>
        /// Generates a random <see cref="byte"/> value between min and max.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>A random <see cref="byte"/> between min and max.</returns>
        public static byte NextByte(this Random random, byte min = 0, byte max = byte.MaxValue)
        {
            return (byte)random.Next(min, max == byte.MaxValue ? byte.MaxValue + 1 : max);
        }

        /// <summary>
        /// Generates a random <see cref="float"/> value between min and max.
        /// </summary>
        /// <param name="random">An instance of <see cref="Random"/>.</param>
        /// <param name="min">The minimum value.</param>
        /// <param name="max">The maximum value.</param>
        /// <returns>A random <see cref="float"/> between min and max.</returns>
        public static float NextFloat(this Random random, float min = 0.0f, float max = 1.0f)
        {
            return (float)random.NextDouble() * (max - min) + min;
        }