Skip to content

Instantly share code, notes, and snippets.

@DiogoRibeiro7
DiogoRibeiro7 / binomial.py
Created April 11, 2019 23:30 — forked from rougier/binomial.py
A fast way to calculate binomial coefficients in python (Andrew Dalke)
def binomial(n, k):
"""
A fast way to calculate binomial coefficients by Andrew Dalke.
See http://stackoverflow.com/questions/3025162/statistics-combinations-in-python
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
@DiogoRibeiro7
DiogoRibeiro7 / randomvariate.py
Created May 6, 2019 20:50 — forked from rsnemmen/randomvariate.py
Rejection method for random number generation / Python
def randomvariate(pdf,n=1000,xmin=0,xmax=1):
"""
Rejection method for random number generation
===============================================
Uses the rejection method for generating random numbers derived from an arbitrary
probability distribution. For reference, see Bevington's book, page 84. Based on
rejection*.py.
Usage:
>>> randomvariate(P,N,xmin,xmax)