31 lines
No EOL
768 B
C#
31 lines
No EOL
768 B
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
|
|
namespace DefaultNamespace
|
|
{
|
|
public static class StackExtensions
|
|
{
|
|
private static Random random = new Random();
|
|
|
|
public static void Shuffle<T>(this Stack<T> stack)
|
|
{
|
|
List<T> list = new List<T>(stack);
|
|
stack.Clear();
|
|
|
|
// Fisher-Yates shuffle
|
|
for (int i = list.Count - 1; i > 0; i--)
|
|
{
|
|
int j = random.Next(i + 1);
|
|
(list[i], list[j]) = (list[j], list[i]); // Swap elements
|
|
}
|
|
|
|
// Push shuffled elements back to stack
|
|
foreach (T item in list)
|
|
{
|
|
stack.Push(item);
|
|
}
|
|
}
|
|
}
|
|
} |