import random


def toggle(chromosome, length, probability):
    """Return a mutated chromosome made through inverting a random bit.

    This chooses a random bit in a binary string and toggles it.

    Args:
        chromosome (int): The source chromosome for mutation.
        length (int): The number of bits in use by the encoding scheme.
        probability (float): The mutation rate or the probability that an
            allele will be mutated.

    Returns:
        int: A mutant chromosome.
    """
    for locus in range(0, length):
        if random.random() < probability:
            chromosome = chromosome ^ 2 ** locus

    return chromosome