import random


def swap_permutation(permutation, numswaps):
    """
    Swap numswaps pairs of integers in the permutation
    Input permutation should be an index vector (e.g. a permutation of
    the integers from 0 to dimension-1 of the vector).
    """
    newperm = permutation.copy()
    for x in range(numswaps):
        a, b = random.sample(range(len(permutation)), 2)
        newperm[a],newperm[b] = newperm[b],newperm[a]
    return newperm