Shuffle the elements contained in the array. - CSharp System

CSharp examples for System:Array Shuffle

Description

Shuffle the elements contained in the array.

Demo Code


using System.Linq;
using System;/* w  ww.  j  a  v  a 2s  .  co  m*/
using System.Collections.Generic;
using System.Collections;
using UnityEngine;

public class Main{
        /// <summary>
        /// Shuffle the elements contained in the array. 
        /// </summary>
        public static void Shuffle<T>(this T[] self)
        {
            if (self == null)
            {
                Debug.LogException(new ArgumentNullException("self"));
                return;
            }

            for (int i = self.Length; i > 1; i--)
            {
                int j = UnityEngine.Random.Range(0, i);
                T tmp = self[j];
                self[j] = self[i - 1];
                self[i - 1] = tmp;
            }
        }
}

Related Tutorials