2

I have a requirement to generate numbers between two limits based on one of the statistical distributions using python.

Generally speaking, in python I can pick one number from an array based on a predefined probability distribution like this:

import random
number_list = [1,2,3,4,5,6,7,8,9,10,11,12]
list_weights = (1,5,40,30,20,20,15,12,10,5,2,1)
adraw = random.choices(number_list, weights=list_weights, k=1)[0]
adraw

The above example will give a number from the supplied based on a left skewed distribution that I manually set using the list_weights probabilities.

I want to expand the above concept so that I can draw a number between 2000000 to 1800000000 microseconds (which is microseconds distribution between 2 secs to 30 mins). I started with

random.randint(2000000,1800000000)

which is totally random.

However, what I am after is to be able to draw these numbers from the range above according to one of the statistical distributions. For example (ordered according to priority):

  • normal distribution (narrow and wide)
  • left skewed distribution (positive skew)
  • right skewed distribution (negative skew)
  • exponential distribution
  • inverse exponential distribution
  • I would really want to be able to draw numbers based on any of the probability distributions drawn in the two graphs here: https://en.wikipedia.org/wiki/Beta_distribution, which seems to be able to produce using two variables Alpha and Beta!

Most of the code I found online does not seem to offer the option of setting the lower and upper limits of the number range to draw from, which is where I am struggling a lot!

Thanks

Sal
  • 331
  • 2
  • 9
  • The reason normal dist for example doesn't have upper and lower bounds is that it is not bounded. You can bound it you self, e.g. `num = draw(...); num = min(max(num, lower_limit), upper_limit)` – Tom Ron Jun 05 '22 at 17:19

0 Answers0