与以下问题类似:
Create random numbers with left skewed probability distribution
通过说明最大值和方差,我想从给定范围内采样整数。
例如,对于范围-{0,1,...,1000}(又名range(1001)
),最大值为100,因此采样的数字很可能在[90- 110],则不太可能被采样的数字是[80-89]和[111-120]等。
答案 0 :(得分:1)
以下代码可以做到这一点:
import scipy.stats as ss
import numpy as np
import matplotlib.pyplot as plt
center = 100
n = 1001
std = 20
x = np.arange(0, n)
prob = ss.norm.pdf(x,loc=center, scale = std )
prob = prob / prob.sum() #normalize the probabilities so their sum is 1
nums = np.random.choice(x, 20, p=prob)
nums