Sampling random numbers from any probability distribution you want

Oscar Nieves
4 min readJul 31, 2021

A guide using simple Python code.

Random numbers sampled from a positive cosine probability distribution

In my previous story (https://oscarnieves100.medium.com/simulating-correlated-random-variables-in-python-c3947f2dbb10) I discussed how to simulate correlated random numbers from a normal probability distribution. We focused on a normal distribution because it is well known, and standard methods of sampling exist.

But what if you wanted to create your own probability distribution? What if I told you that you don’t need to be constrained by any of the well-known probability distributions out there? Where there is a will, there is a way, and what I am about to show you is very simple yet very powerful.

First, let us establish some rules for creating a probability distribution p(x):

  1. p(x) ≥ 0, that is: p(x) must be positive or zero for all x values, where x is a real number. p(x) must never be negative regardless of the circumstances (negative probabilities make no sense).
  2. The integral of p(x) over all possible x must be equal to one. If p(x) is discrete, then by definition the sum of all p(x) for all x must be 1. We define this mathematically as Σp(x) = 1. In general, we can ensure that a probability distribution always obeys this property as follows: first, we create an “un-normalised” distribution…

--

--

Oscar Nieves
Oscar Nieves

Written by Oscar Nieves

I write stories about applied math, physics and engineering.

No responses yet