To generate a geometric with probability p
of success on each trial, given a function rand
which returns a uniform(0,1) result, pseudocode is:
define geometric(p)
return ceiling(ln(1-rand) / ln(1-p))
This yields how many trials until the first success. If you want the alternate definition of a geometric (how many failures prior to the first success) subtract 1 or use floor
instead of ceiling
.