public static void FullyRandomizeLocations(Location[] locations)
{
if (locations == null)
{
throw new ArgumentNullException("locations");
}
// This code does a full randomization of the destination locations without creating a new array.
// If we have 3 items, for example, it will first determine which one of the 3 will be in the last
// place, swapping items if needed. Then, it will chose which one of the first 2 items is put at the
// second place. And, as everything works by swaps, the item in the first position is obviously the
// only one that remains, that's why the i>0 is used instead of i>=0.
int count = locations.Length;
for (int i = count - 1; i > 0; i--)
{
int value = GetRandomValue(i + 1);
if (value != i)
{
Location.SwapLocations(locations, i, value);
}
}
}