This is only my opinion of course and I am, like always, braced for someone to come along and say "There is one!" lol . But at present I am not aware if there is one, so... I am just writing to display my attempt at one. I think I have made it quite, well generic, in the fact that I have used generics. I would love to see any Maths gurus out there put me to shame by cutting my lines of code in half or less lol, using some real efficient maths. I need to learn more about that topic. So complex.
Any way here is my attempt. It looks long to me, but I as I was once told, "Don't let perfection get in the way of GOOD ENOUGH."
Hide Code [-] public static T[] RandomizeArray<T>(T[] listIn)
{
Random r = new Random(DateTime.Now.Millisecond);
T[] destination = new T[listIn.Length];
T[] stagingList = listIn;
for (int i = 0; i < listIn.Length; i++)
{
T[] tempList = new T[stagingList.Length == 1 ? stagingList.Length : stagingList.Length - 1];
int randomNumber = r.Next(stagingList.Length);
destination[i] = randomNumber == 0 ? stagingList[0] : stagingList[randomNumber - 1];
int item = 0;
for (int j = 0; j < stagingList.Length; j++)
{
if (j != ((randomNumber == 0) ? 0 : randomNumber - 1))
{
tempList[item] = stagingList[j];
item++;
}
}
stagingList = tempList;
}
return destination;
}
{..} Click Show Code
The example implementation is as follows:
Hide Code [-]
int[] test = new int[10];
for (int i = 0; i < 10; i++)
{
test[i] = i;
}
int[] newList = RandomizeArray<int>(test);
foreach (int j in newList)
{
Console.WriteLine(j.ToString());
}
newList = RandomizeArray<int>(test);
Console.ReadLine();
{..} Click Show Code
Cheers,
Andrew :-)