-3

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.

Martin
  • 121
  • 2
  • Welcome to SO Martin! The community will gladly help you with coding problems you encounter as you work on an issue. However, you need to provide the code that you have written and the problem you are encountering. Normally, we don't write it for you. – AlG Jul 01 '15 at 18:30
  • The problem is so fundamental, that I can't even Start coding. – Martin Jul 01 '15 at 18:34

2 Answers2

4

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.html

random.triangular(low, high, mode)

Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds.

Histogram made with matplotlib:

import matplotlib.pyplot as plt
import random
bins = [0.1 * i for i in range(12)]
plt.hist([random.triangular(0,1,0) for i in range(2500)], bins)

enter image description here

Community
  • 1
  • 1
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
2

For denormalized PDF with density

1-x, in the range [0...1)

normalization constant is 1/2

CDF is equal to 2x-x^2

Thus, sampling is quite obvious

r = 1.0 - math.sqrt(random.random())

Sample program produced pretty much the same plot

import math
import random
import matplotlib.pyplot as plt

bins = [0.1 * i for i in range(12)]
plt.hist([(1.0 - math.sqrt(random.random())) for k in range(10000)], bins)
plt.show()

UPDATE

let's denote S to be an integral, and S_a^b is definite integral from a to b.

So

Denormalized PDF(x) = 1-x

Normalization:

N = S_0^1 (1-x) dx = 1/2

Thus, normalized PDF

PDF(x) = 2*(1-x)

Let's compute CDF

CDF(x) = S_0^x PDF(x) dx = 2x - x*x

Checking: CDF(0) = 0, CDF(1) = 1

Sampling is via inverse CDF method, by solving for x

CDF(x) = U(0,1)

where U(0,1) is uniform random in [0,1)

This is simple quadratic equation with solution

x = 1 - sqrt(1 - U(0,1)) = 1 - sqrt(U(0,1))

which translated directly into Python code

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64