Fast arbitrary distribution random sampling (inverse transform sampling)

前端 未结 5 626
名媛妹妹
名媛妹妹 2020-11-28 07:00

The random module (http://docs.python.org/2/library/random.html) has several fixed functions to randomly sample from. For example random.gauss

5条回答
  •  不知归路
    2020-11-28 07:18

    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:

    • http://en.wikipedia.org/wiki/Inverse_transform_sampling

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

    • Pythonic way to select list elements with different probability

提交回复
热议问题