Sample integers from truncated geometric distribution
The simple answer is: pick a random number from geometric distribution and return mod n.
Eg: random.geometric(p)%n
P(x) = p(1-p)^x+ p(1-p)^(x+n) + p(1-p)^(x+2n) ....
= p(1-p)^x *(1+(1-p)^n +(1-p)^(2n) ... )
Note that second part is a constant for a given p and n. The first part is geometric.
What is a good way to sample integers in the range {0,...,n-1} according to (a discrete version of) the exponential distribution?  random.expovariate(lambd) returns a real number  from 0 to positive infinity.
Update. Changed title to make it more accurate.