Fisher-Yates shuffle

This commit is contained in:
Marco 2025-04-15 15:19:36 +02:00
commit aa838aeda1
6 changed files with 176 additions and 7 deletions

View file

@ -0,0 +1,45 @@
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<T> Shuffle<T>(this IEnumerable<T> 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.HasValue && 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;
}
}
}