'Return a randomly selected sample from this probability distribution.
The probability of returning each sample ``samp`` is equal to
``self.prob(samp)``.'

def generate(self):

    p = random.random()
    p_init = p
    for sample in self.samples():
        p -= self.prob(sample)
        if (p <= 0):
            return sample
    if (p < 0.0001):
        return sample
    if self.SUM_TO_ONE:
        warnings.warn((u'Probability    distribution    %r    sums    to    %r;    generate()    is    returning    an    arbitrary    sample.' % (self, (p_init - p))))
    return random.choice(list(self.samples()))
