How to generate random numbers to satisfy a specific mean and median in python?

后端 未结 5 1337
独厮守ぢ
独厮守ぢ 2021-02-14 10:59

I would like to generate n random numbers e.g., n=200, where the range of possible values is between 2 and 40 with a mean of 12 and median is 6.5.

I searche

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-14 11:04

    Ok, you're looking at the distribution which has no less than 4 parameters - two of those defining range and two responsible for required mean and median.

    I could think about two possibilities from the top of my head:

    1. Truncated normal distribution, look here for details. You have already range defined, and have to recover μ and σ from mean and median. It will require solving couple of nonlinear equation, but quite doable in python. Sampling could be done using https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.truncnorm.html

    2. 4-parameters Beta distribution, see here for details. Again, recovering α and β in Beta distribution from mean and median will require solving couple of non-linear equations. Knowing them sampling would be easy via https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.beta.html

    UPDATE

    Here how you could do it for truncated normal going from mean to mu: Truncated normal with a given mean

提交回复
热议问题