2019년 4월 13일 토요일

# Fisher–Yates shuffle C# 버전

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;
using System.Collections;

/// <summary>
/// Fisher–Yates shuffle 알고리즘을 이용한 셔플.
/// </summary>
public static class Utility {

 public static T[] ShuffleArray<T>(T[] arr, int seed)
    {
        System.Random rand = new System.Random(seed);
        
        for(int idx = 0; idx < arr.Length-1; idx++)
        {
            int randNum = rand.Next(0, arr.Length);
            T tmp = arr[randNum];
            arr[randNum] = arr[idx];
            arr[idx] = tmp;
        }
        return arr;
    }
}

댓글 없음:

댓글 쓰기