I am looking to randomly shuffle a list/array using a key. I want to be able to repeat the same random order using the key.
So I will randomly generate a numeric key from say 1 to 20 then use that key to try and randomly shuffle the list.
I first tried just using the key to keep iterating through my list, decrementing the key until=0, then grabbing whatever element I am on, removing it and adding it to my shuffled array. The result is kind of random but when the arrays are small (which most of mine will be) and/or the key is small it doesn't end up shuffling... seems to be more of a shift.
I have to be able to determine what order the
Here is some sample code in csharp of :
public static TList<VoteSetupAnswer> ShuffleListWithKey(TList<VoteSetupAnswer> UnsortedList, int ShuffleKey)
{
TList<VoteSetupAnswer> SortedList = new TList<VoteSetupAnswer>();
int UnsortedListCount = UnsortedList.Count;
for (int i = 0; i < UnsortedListCount; i++)
{
int Location;
SortedList.Add(OneArrayCycle(UnsortedList, ShuffleKey, out Location));
UnsortedList.RemoveAt(Location);
}
return SortedList;
}
public static VoteSetupAnswer OneArrayCycle(TList<VoteSetupAnswer> array, int ShuffleKey, out int Location)
{
Location = 0;
if (ShuffleKey == 1)
{
Location = 0;
return array[0];
}
else
{
for (int x = 0; x <= ShuffleKey; x++)
{
if (x == ShuffleKey)
return array[Location];
Location++;
if (Location == array.Count)
Location = 0;
}
return array[Location];
}
}