(C#) ShuffleArray

C#/Winform 2018. 2. 24. 13:37
728x90
728x170
        /// <summary>
        /// Array 내용을 랜덤하게 뽑아오되 앞에서 뽑힌내용은 제외하고 뽑아올때 사용됩니다.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static T[] ShuffleArray<T>(T[] array, int seed)
        {
            System.Random r = new System.Random(seed);

            for (int i = 0; i < array.Length - 1; i++)
            {
                int randomIndex = r.Next(i, array.Length);
                T tempItem = array[randomIndex];
                array[randomIndex] = array[i];
                array[i] = tempItem;
            }
           
            return array;
        }

728x90
그리드형
Posted by kjun.kr
,