Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select one element from a list using python following the normal distribution

I would like to select one element from a list using python following the normal distribution. I have a list, e.g.,

alist = ['an', 'am', 'apple', 'cool', 'why']

For example, according to the probability density function (PDF) of normal distribution, the 3rd element in the given list should have the largest probability to be chosen.Any suggestions?

like image 524
Frank Wang Avatar asked May 16 '26 01:05

Frank Wang


2 Answers

from random import normalvariate

def normal_choice(lst, mean=None, stddev=None):
    if mean is None:
        # if mean is not specified, use center of list
        mean = (len(lst) - 1) / 2

    if stddev is None:
        # if stddev is not specified, let list be -3 .. +3 standard deviations
        stddev = len(lst) / 6

    while True:
        index = int(normalvariate(mean, stddev) + 0.5)
        if 0 <= index < len(lst):
            return lst[index]

then

alist = ['an', 'am', 'apple', 'cool', 'why']
for _ in range(20):
    print(normal_choice(alist))

gives

why
an
cool
cool
cool
apple
cool
apple
am
am
apple
apple
apple
why
cool
cool
cool
am
am
apple
like image 94
Hugh Bothwell Avatar answered May 17 '26 14:05

Hugh Bothwell


Are you sure you really want a normal distribution, you could look at a Beta Distribution, which would probably give you what you need, e.g.:

>>> import random
>>> from collections import Counter
>>> alist = ['an', 'am', 'apple', 'cool', 'why']
>>> Counter(alist[int(random.betavariate(2, 2)*len(alist))] for _ in range(100))
Counter({'am': 20, 'an': 9, 'apple': 34, 'cool': 23, 'why': 14})
>>> Counter(alist[int(random.betavariate(10, 10)*len(alist))] for _ in range(1000))  
Counter({'am': 183, 'apple': 621, 'cool': 189, 'why': 4, 'an': 3})
like image 34
AChampion Avatar answered May 17 '26 15:05

AChampion