■ List<T> 클래스를 사용해 리스트를 임의로 섞는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Collections.Generic; #region 섞기 - Shuffle<TItem>(sourceList) /// <summary> /// 섞기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="sourceList">소스 리스트</param> /// <returns>섞은 후 리스트</returns> public List<TItem> Shuffle<TItem>(List<TItem> sourceList) { List<TItem> targetList = new List<TItem>(); Random random = new Random(DateTime.Now.Millisecond); while(sourceList.Count > 0) { int index = random.Next(0, sourceList.Count); targetList.Add(sourceList[index]); sourceList.RemoveAt(index); } return targetList; } #endregion |