Shuffle List Range - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Shuffle List Range

Demo Code



public class Main{
        public static void ShuffleRange<T>(this IList<T> list, int startIndex, int endIndex) {
         for (int i = endIndex; i > startIndex; i--) {
            int num2 = UnityEngine.Random.Range(startIndex, i + 1);
            T local = list[num2];/*from  w w w. j  a va 2s  .co m*/
            list[num2] = list[i];
            list[i] = local;
         }
      }
        public static T Random<T>(this IList<T> collection) {
         if ((collection == null) || (collection.Count == 0)) {
            return default(T);
         }
         int num = UnityEngine.Random.Range(0, collection.Count);
         return collection[num];
      }
}

Related Tutorials