Weighed numpys random.choice for intervals

Since all distributions you wish to pick from are uniform, I would suggest the following setup.

interval_p = np.array([0.1, 0.15, 0.2, 0.2, 0.15, 0.1, 0.05, 0.03, 0.02])
interval_lo = np.array([1,5,10,25,50,100,200,300,500])
interval_hi = np.concatenate([lo[1:], [1000]])
interval_width = interval_hi - interval_lo

n = 20
c = np.random.choice(len(interval_p), p=interval_p, size=n)
results = interval_lo[c] + np.random.uniform(size=n) * interval_width[c]

This works because we can rescale the uniform distribution r on [0, 1] to any uniform distribution on [a, b] using a + r*(b-a).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top