def weighted_replicate(seq, weights, n):
assert len(seq) == len(weights)<EOL>weights = normalize(weights)<EOL>wholes = [int(w*n) for w in weights]<EOL>fractions = [(w*n) % <NUM_LIT:1> for w in weights]<EOL>return (flatten([x] * nx for x, nx in zip(seq, wholes))<EOL>+ weighted_sample_with_replacement(seq, fractions, n - sum(wholes)))<EOL>
Return n selections from seq, with the count of each element of
    seq proportional to the corresponding weight (filling in fractions
    randomly).
    >>> weighted_replicate('ABC', [1,2,1], 4)
    ['A', 'B', 'B', 'C']