Gabriel Gabriel - 1 year ago 143
Python Question

Fast arbitrary distribution random sampling


module ( has several fixed functions to randomly sample from. For example
will sample random point from a normal distribution with a given mean and sigma values.

I'm looking for a way to extract a number
of random samples between a given interval using my own distribution as fast as possible in
. This is what I mean:

def my_dist(x):
# Some distribution, assume c1,c2,c3 and c4 are known.
f = c1*exp(-((x-c2)**c3)/c4)
return f

# Draw N random samples from my distribution between given limits a,b.
N = 1000
N_rand_samples = ran_func_sample(my_dist, a, b, N)

is what I'm after and
a, b
are the limits from which to draw the samples. Is there anything of that sort in

Answer Source

You need to use Inverse transform sampling method to get random values distributed according to a law you want. Using this method you can just apply inverted function to random numbers having standard uniform distribution in the interval [0,1].

After you find the inverted function, you get 1000 numbers distributed according to the needed distribution this obvious way:

[inverted_function(random.random()) for x in range(1000)]

More on Inverse Transform Sampling:

Also, there is a good question on StackOverflow related to the topic:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download