mardi 25 août 2020

Python Generate Non Uniform Randint

If I use python random's randint function, I seem to be getting a uniform distribution as below.

How do I make this a non-uniform randint?

I do not care about specifying a specific range like this: Generate random numbers with a given (numerical) distribution (numpy.random.choice/ random.choice), the answer can have any distribution over the range.

Thank you for your time.

Eg:

#Counter({2: 10760, 6: 190364, 4: 40092, 0: 160068, 7: 99936, 3: 99885, 8: 99845, 9: 99725, 5: 99675, 1: 99650})

Current Example Code:

from collections import Counter
from random import randint
list_size=1000000
random_list=list(sorted([randint(0,9) for x in range(list_size)]))
Counter(random_list)
#Counter({2: 100760, 6: 100364, 4: 100092, 0: 100068, 7: 99936, 3: 99885, 8: 99845, 9: 99725, 5: 99675, 1: 99650})

Things I have tried:

def really_random(start,stop):
    random_array_size=random.randint(3,10)
    random_array_choice=random.randint(0,random_array_size-1)
    random_value=[random.randint(start,stop) for x in range(random_array_size)][random_array_choice]
    return random_value

def really_random_test(start,stop):
    list_size = 100000
    random_list = list(sorted([really_random(start, stop) for x in range(list_size)]))
    print(Counter(random_list))

still pretty uniform

#Counter({7: 10094, 9: 10066, 3: 10044, 1: 10027, 5: 10012, 8: 10012, 0: 10009, 6: 9985, 2: 9878, 4: 9873})



Aucun commentaire:

Enregistrer un commentaire