using System; using System.Collections.Generic; using System.Linq; using Godot; namespace Cirno.Scripts.Utils; public static class ShuffleExtensions { //private static Random rng = new Random(); public static IEnumerable Shuffle(this IEnumerable source, int? desiredLength = null) { if (source == null) throw new ArgumentNullException(nameof(source)); var list = source.ToList(); int originalCount = list.Count; if (originalCount == 0) yield break; // Repeat elements to meet the desired length if (desiredLength > originalCount) { int countToAdd = desiredLength.Value - originalCount; for (int i = 0; i < countToAdd; i++) { list.Add(list[i % originalCount]); } } // Fisher-Yates shuffle for (int i = list.Count - 1; i > 0; i--) { //int j = rng.Next(i + 1); int j = GD.RandRange(0, i); (list[i], list[j]) = (list[j], list[i]); } // Return only up to desired length if specified foreach (var item in list.Take(desiredLength ?? list.Count)) { yield return item; } } }