첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ IList<T> 인터페이스를 사용해 리스트를 임의로 섞는 방법을 보여준다.

TestProject.zip
0.00MB

▶ ThreadSafeRandom.cs

namespace TestProject;

/// <summary>
/// 쓰레드 안전 난수기
/// </summary>
public static class ThreadSafeRandom
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Field
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region Field

    /// <summary>
    /// 난수기
    /// </summary>
    [ThreadStatic]
    private static Random _random;

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

    #region 난수 값 - RandomValue

    /// <summary>
    /// 난수 값
    /// </summary>
    public static Random RandomValue
    {
        get
        {
            return _random ?? (_random = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId)));
        }
    }

    #endregion
}

 

▶ ListExtension.cs

namespace TestProject;

/// <summary>
/// 리스트 확장
/// </summary>
public static class ListExtension
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

    #region 섞기 - Shuffle<TItem>(sourceList)

    /// <summary>
    /// 섞기
    /// </summary>
    /// <typeparam name="TItem">항목 타입</typeparam>
    /// <param name="sourceList">소스 리스트</param>
    public static void Shuffle<TItem>(this IList<TItem> sourceList)
    {
        int count = sourceList.Count;

        while(count > 1)
        {
            count--;

            int index = ThreadSafeRandom.RandomValue.Next(count + 1);

            TItem value = sourceList[index];

            sourceList[index] = sourceList[count];
            sourceList[count] = value;
        }
    }

    #endregion
}

 

▶ Program.cs

namespace TestProject;

/// <summary>
/// 프로그램
/// </summary>
class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

    #region 프로그램 시작하기 - Main()

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        List<int> list = new List<int>(Enumerable.Range(1, 45));

        Console.WriteLine("로또 추출 번호");

        for(int i = 0; i < 5; i++)
        {
            list.Shuffle();

            Console.WriteLine("    {0}", string.Join(", ", list.GetRange(0, 6)));
        }
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요