import random


def sample(table, amino_acid):
    """Sample a codon for a given amino acid probabilistically, based on its
    codon usage frequency.

    :param table: a codon usage table.
    :type table: dict
    :param amino_acid: a single-character string representing an amino acid.
    :type amino_acid: str
    :return: a codon encoding the supplied amino acid, sampled
        probabilistically.
    :rtype: str
    """
    cum_freq = 0
    rand_val = random.random()

    for codon, freq in table[amino_acid].items():
        cum_freq += freq

        if cum_freq > rand_val:
            return codon

    return None