Rnen10

C# List Shuffle

Aug 23rd, 2022 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.49 KB | Source Code | 0 0
  1. using System;
  2. using System.Random;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6.  
  7. public static class ListExtensions  {
  8.     public static void Shuffle<T>(this IList<T> list) {
  9.         Random rnd = new Random();
  10.         for (var i = 0; i < list.Count; i++)
  11.             list.Swap(i, rnd.Next(i, list.Count));
  12.     }
  13.  
  14.     public static void Swap<T>(this IList<T> list, int i, int j) {
  15.         var temp = list[i];
  16.         list[i] = list[j];
  17.         list[j] = temp;
  18.     }
  19. }
Tags: C# Unity
Add Comment
Please, Sign In to add comment