dimanche 10 octobre 2021

Linear distribution of numbers in python (higher numbers are rare, low numbers are common) random weighted chance

I'm looking to create a random function where 100 is the rarest number while 1 is the most common. It should be a linear distribution so for example the function returning 100 is the lowest chance, then 99 is the second lowest, then 98 is the third lowest and so forth. I've tried this code below:

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)

This does return a lot more low numbers through 1- 10, but after that, since its an exponential function, numbers 10-100 have a very similar count.

Is there any way to create a linear distribution like below: Linear distribution of numbers




Aucun commentaire:

Enregistrer un commentaire