/// <summary> /// Yields a random sequence of elements. /// </summary> /// <typeparam name="T">The type of elements.<typeparam> /// <param name="list">The set of elements to randomize.</param> /// <param name="options">The parameters of randomization.</param> /// <param name="randomizer">The random index strategy.</param> /// <returns>The random sequence of elements.</returns> public static IEnumerable <T> Randomize <T>(this IList <T> list, RandomizationOptions options, RandomIndex randomizer) { int n = list.Count; int size = n; while (size > 0) { int index = randomizer(size); T element = list[index]; if (options.HasFlag(RandomizationOptions.NoReplacement)) { size--; Hide(index); } else { if (options.HasFlag(RandomizationOptions.NoConsecutiveValues)) { size = n - 1; Hide(index); } } yield return(element); } #region PIPELINE void Hide(int i) => list.Swap(i, size); #endregion }
/// <summary> /// Yields a random sequence of elements. /// </summary> /// <typeparam name="T">The type of elements.<typeparam> /// <param name="list">The set of elements to randomize.</param> /// <param name="options">The parameters of randomization.</param> /// <returns>The random sequence of elements.</returns> public static IEnumerable <T> Randomize <T>(this IList <T> list, RandomizationOptions options) => Randomize(list, options, UnityRandom);
/// <summary> /// Yields a random sequence of elements. /// </summary> /// <typeparam name="T">The type of elements.<typeparam> /// <param name="collection">The set of elements to randomize.</param> /// <param name="options">The parameters of randomization.</param> /// <param name="randomizer">The random index strategy.</param> /// <returns>The random sequence of elements.</returns> public static IEnumerable <T> Randomize <T>(this IEnumerable <T> collection, RandomizationOptions options, RandomIndex randomizer) => Randomize(collection.ToList(), options, randomizer);
/// <summary> /// Yields a random sequence of elements. /// </summary> /// <typeparam name="T">The type of elements.<typeparam> /// <param name="collection">The set of elements to randomize.</param> /// <param name="options">The parameters of randomization.</param> /// <returns>The random sequence of elements.</returns> public static IEnumerable <T> Randomize <T>(this IEnumerable <T> collection, RandomizationOptions options) => Randomize(collection, options, UnityRandom);