Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to get weighted random choice in Python?

  • Last Updated : 05 Sep, 2020

Weighted random choices mean selecting random elements from a list or an array by the probability of that element. We can assign a probability to each element and according to that element(s) will be selected. By this, we can select one or more than one element from the list, And it can be achieved in two ways.

  1. By random.choices()
  2. By numpy.random.choice()

Using Random.choices() method

The choices() method returns multiple random elements from the list with replacement. You can weigh the possibility of each result with the weights parameter or the cum_weights parameter.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Syntax : random.choices(sequence, weights=None, cum_weights=None, k=1)

Parameters :
1. sequence is a mandatory parameter that can be a list, tuple, or string.
2. weights is an optional parameter which is used to weigh the possibility for each value.
3. cum_weights is an optional parameter which is used to weigh the possibility for each value but in this the possibility is accumulated
4. k is an optional parameter that is used to define the length of the returned list.

Example 1:


import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(
  sampleList, weights=(10, 20, 30, 40, 50), k=5)


[200, 300, 300, 300, 400]

You can also use cum_weight parameter. It stands for commutative weight. By default, if we will use the above method and send weights than this function will change weights to commutative weight. So to make the program fast use cum_weight. Cumulative weight is calculated by the formula:

cum weight = weight of previous element + its own weight

let the relative weight of 5 elements are [5,10,20,30,35]

than there cum_weight will be [5,15,35,65,100]



import random
sampleList = [100, 200, 300, 400, 500]
randomList = random.choices(
  sampleList, cum_weights=(5, 15, 35, 65, 100), k=5)


[500, 500, 400, 300, 400]

Using numpy.random.choice() method

If you are using Python older than 3.6 version, than you have to use NumPy library to achieve weighted random numbers. With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array.

Syntax: numpy.random.choice(list,k, p=None)

List: It is the original list from you have select random numbers.

k: It is the size of the returning list. i.e, the number of elements you want to select.

p: It is the probability of each element.

Note: the total sum of the probability of all the elements should be equal to 1.



from numpy.random import choice
sampleList = [100, 200, 300, 400, 500]
randomNumberList = choice(
  sampleList, 5, p=[0.05, 0.1, 0.15, 0.20, 0.5])


[200 400 400 200 400]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!