I'd like to generate random numbers that follow a dropping linear frequency distribution, take n=1-x for an example.
The numpy library however seems to offer only more complex distributions.
I'd like to generate random numbers that follow a dropping linear frequency distribution, take n=1-x for an example.
The numpy library however seems to offer only more complex distributions.
So, it turns out you can totally use
random.triangular(0,1,0)
for this. See documentation here: https://docs.python.org/2/library/random.htmlHistogram made with
matplotlib
:For denormalized PDF with density
normalization constant is 1/2
CDF is equal to
2x-x^2
Thus, sampling is quite obvious
Sample program produced pretty much the same plot
UPDATE
let's denote
S
to be an integral, andS_a^b
is definite integral froma
tob
.So
Normalization:
Thus, normalized PDF
Let's compute CDF
Checking:
CDF(0) = 0
,CDF(1) = 1
Sampling is via inverse CDF method, by solving for
x
where
U(0,1)
is uniform random in [0,1)This is simple quadratic equation with solution
which translated directly into Python code