import random


def sample_coal_count(a, t, n):
    """
    Sample the number lineages present starting after generations 't' starting
    with 'a' lineages and population size 'n'
    """

    t2 = 0.0
    b = a
    while b > 1:
        rate = b * (b - 1) / 2.0 / n
        t2 += random.expovariate(rate)
        if t2 < t:
            b -= 1
        else:
            break
    return b