<p>我想创建一个随机函数,其中100是最稀有的数字,而1是最常见的数字。它应该是一个线性分布,例如返回100的函数是最低概率,然后99是第二低概率,然后98是第三低概率,依此类推。我已在下面尝试了此代码:</p>
<pre><code>def getPercentContent():
minPercent = 5
maxPercent = 102 # this will return 101 as highest number
power = 1.5 # higher number, more concentration to lower numbers
num = math.floor(minPercent+(maxPercent-minPercent)*random.random()**power)
return str(num)
</code></pre>
<p>通过1-10,这确实会返回更多的低数值,但在这之后,因为它是指数函数,所以10-100的数值非常相似</p>
<p>有没有办法创建如下所示的线性分布:<a href="https://i.stack.imgur.com/GcFGv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GcFGv.png" alt="Linear distribution of numbers"/></a></p>