def geometric(p): if p <= 0 or p > 1: raise ValueError('p must be in the interval (0.0, 1.0]') if p == 1: return 1 return int(math.log1p(-random.random()) / math.log1p(-p)) + 1