/// <summary> /// Swaps three randomly chosen elements of the given <paramref name="permutation"/> array. /// </summary> /// <exception cref="ArgumentException">Thrown when the <paramref name="permutation"/> contains less than 3 elements.</exception> /// <remarks> /// It is implemented such that first 3 positions are randomly chosen in the interval [0;N) with N = length of the permutation with all positions being distinct from each other. /// Then position 1 is put in place of position 3, position 2 is put in place of position 1 and position 3 is put in place of position 2. /// </remarks> /// <param name="random">The random number generator.</param> /// <param name="permutation">The permutation to manipulate.</param> public static void Apply(IRandom random, Permutation permutation) { if (permutation.Length < 3) { throw new ArgumentException("Swap3Manipulator: The permutation must be at least of size 3.", "permutation"); } int index1, index2, index3; do { index1 = random.Next(permutation.Length); index2 = random.Next(permutation.Length); index3 = random.Next(permutation.Length); } while (index1 == index2 || index2 == index3 || index1 == index3); permutation.Swap(index1, index2); permutation.Swap(index2, index3); }
public static void Apply(Permutation permutation, int index1, int index2) { permutation.Swap(index1, index2); }