import random


def swap(chromosome, probability):
    """Return a mutated chromosome made through swap mutation.

    This chooses two random points in a list and swaps their values. It is
    suitable for use with list, value, and permutation encoding, but designed
    for the latter.

    Args:
        chromosome (List): The source chromosome for mutation.
        probability (float): The mutation rate or the probability that an
            allele will be mutated.

    Returns:
        List: A mutant chromosome.

    """
    mutant = list(chromosome)
    changes = 0
    offset = probability

    for locus1 in range(0, len(chromosome)):
        if random.random() < offset:
            locus2 = locus1
            while locus2 == locus1:
                locus2 = random.randint(0, len(chromosome) - 1)

            mutant[locus1], mutant[locus2] = mutant[locus2], mutant[locus1]
            changes += 2
            offset = probability / changes

    return mutant