I want to randomly permute a finite list in the most effective and efficient way in C#. My attempt is as follows.
/*===================================*
* Compile it to produce Shuffle.exe *
* ==================================*/
using System;
using System.Collections.Generic;
using System.IO;
namespace Shuffle
{
class Program
{
static void Main(string[] args)
{
int Columns = int.Parse(args[0]);
int Rows = int.Parse(args[1]);
int Seed = int.Parse(args[2]);
string OutputFilename = args[3];
List<string> OrderedList = new List<string>();
for (int x = 0; x < Columns; x++)
for (int y = 0; y < Rows; y++)
OrderedList.Add(string.Format("{{{0},{1}}}", x, y));
Random rnd = new Random(Seed);
List<string> ShuffledList = new List<string>();
for (int i = 0; i < Columns * Rows; i++)
{
int x = rnd.Next(OrderedList.Count);
ShuffledList.Add(OrderedList[x]);
OrderedList.RemoveAt(x);
}
using (StreamWriter sw = new StreamWriter(OutputFilename))
{
foreach (string s in ShuffledList)
sw.WriteLine(s);
}
}
}
}
This program will be used in my production. Could you review whether or not my code is already the most efficient and effective in C#?