Reputation: 35
I want to know the method that weighted-random in Python.
1:10%, 2:10%, 3:10%, 4:50%, 5:20%
Then I choose the random number without duplication. How should I code? Generally, we will code below that:
Python
from random import *
sample(range(1,6),1)
Upvotes: 0
Views: 55
Reputation: 51683
If you really wanted a sample-version you can prepare the range accordingly:
nums = [1,2,3,4,5]
w = [10,10,10,50,20] # total of 100%
d = [x for y in ( [n]*i for n,i in zip(nums,w)) for x in y]
a_sample = random.sample(d,k=5)
print(a_sample)
print(d)
Output:
# 5 samples
[4, 2, 3, 1, 4]
# the whole sample input:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
If you just need 1 number you can use random.choices - it is limited to 1 number because its drawing with replacement.
import random
from collections import Counter
# draw and count 10k to show distribution works
print(Counter( random.choices([1,2,3,4,5], weights=[10,10,10,50,20], k=10000)).most_common())
Output:
[(4, 5019), (5, 2073), (3, 1031), (1, 978), (2, 899)]
Using a "sample" w/o replacement and "weighted" is (for me) weired - because you would change the weighting for each successive number because you removed available numbers from the range (thats by feel - my guess would be the math behind tells me its not so).
Upvotes: 0
Reputation: 1000
You should have a look at random.choices (https://docs.python.org/3/library/random.html#random.choices), which allows you to define a weighting, if you are using python 3.6 ore newer
Example:
import random
choices = [1,2,3,4,5]
random.choices(choices, weights=[10,10,10,50,20], k=20)
Output:
[3, 5, 2, 4, 4, 4, 5, 3, 5, 4, 5, 4, 5, 4, 2, 4, 5, 2, 4, 4]
Upvotes: 1
Reputation: 869
Try this:
from numpy.random import choice
list_of_candidates = [1,2,5,4,12]
number_of_items_to_pick = 120
p = [0.1, 0, 0.3, 0.6, 0]
choice(list_of_candidates, number_of_items_to_pick, p=probability_distribution)
Upvotes: 0