mdr141
11/7/2018 - 2:06 AM

Algorithm_フィッシャー–イェーツのシャッフル

using System;
using System.Text;

class Test
{
    static void Main()
    {
        int[] indexArray = { 0, 1, 2, 3, 4, 5 };
        Random rand = new Random();
        
        int end = indexArray.Length;
        
        for (int i_index = end; 1 < i_index; i_index--)
        {
            int j_rand = rand.Next(i_index);
            if (j_rand == i_index - 1) continue;
            int k_temp = indexArray[i_index - 1];
            indexArray[i_index - 1] = indexArray[j];
            indexArray[j_rand] = k_temp;
            
            // TEST
            Console.WriteLine(i_index);
            Console.WriteLine(j_rand == i_index - 1);
            Console.WriteLine(j_rand);
            Console.WriteLine(k_temp);
            Console.WriteLine(indexArray[i_index - 1]);
            Console.WriteLine(indexArray[j_rand]);
            foreach(var item in indexArray)
            {
                Console.Write(item + " | ");
            }
            Console.WriteLine("\r\n-------------------------");
        }
    }
}