import random
import math


def rGeom(p):
    """Generate a geometrically distributed random number. p is the success
    probability. The numbers are in the range {0, 1, 2,...}"""
    # CDF = 1-(1-p)^(k+1) (CDF of geometric distribution)
    # (1-p)^(k+1) = 1-CDF  (... solve for k ...)
    # k+1 = log(1-CDF)/log(1-p)
    # k = (log(1-CDF)/log(1-p)) - 1
    # insert a uniform random number in [0;1] for CDF to 
    # obtain geometrically distributed numbers
    u = random.random()
    if p == 1 : return 0
    return math.ceil( (math.log(1-u,1-p))-1 )