Python random sample() function to Choose Multiple Unique from any Sequence

Posted in

Python random sample() function to Choose Multiple Unique from any Sequence
vinaykhatri

Vinay Khatri
Last updated on December 24, 2024

    This Python tutorial will discuss how to use the Python random.sample() function to choose multiple unique or sample items from a sequence, such as a list, string, and tuple. We will also discuss the methods or techniques to pick random items from a multi-dimensional array in Python.

    Python has an inbuilt random module that provides many methods to generate random numbers or pick random values from a sequential object. sample() is one of the random modules which can pick random unique items from a sequential object and return it as a list.

    Let's say we have a list of 100 student names, and we only need to pick 10 random students. All the 10 random students we pick should not be repeated. In this case, we can use the random.sample() function to and pick 10 random students out of 100.

    Here is the overview of the functions that we will cover in this article.

    Function Description
    random.sample(population, n) It will generate a list of n unique samples from the population sequence.
    random.choices(sequence, n) The choices method randomly picks the n number of items from the sequence , with a possibility of repetitive items.
    random.sample(range(50), n) It will return a list of n random integer numbers between 0 and 50(excluded).
    random.sample(dictionary.items, n) It will return a list of n (key,pair) tuples from a dictionary object.

    How to use the random.sample function in Python?

    The random.sample(population, k, *,counts=None) can accept 3 argument values, and return a list object by selecting k number of random sample items from the sequence population .

    Syntax

    import random
    
    random.sample(population, k)

    Arguments

    • population : It can be a sequential object like list, tuple, and string, from which we want to select random items.
    • k : It is an integer value that decides the number of items that need to be picked randomly from the population sequence.

    Note: The value of k must be less than or equal to the total size of the population, otherwise the Python raises the ValueError( Sample larger than population or is negative ).

    random sample() example

    Suppose we have a list of 10 students' names and we need to randomly select 3 students from the list.

    import random
    
    #ten students
    students = ['Dev', 'Andy', 'Cindy', 'Beth', 'Ester','Rahul', 'Peter', 'Nancy', 'Mark', 'Shiv']
    
    #select 3 random students
    
    lucky_three = random.sample(students, k=3)
    
    print("The three randomly picked students are: ", lucky_three)

    Output

    The three randomly picked students are: ['Peter', 'Andy', 'Cindy']

    Important facts about random.sample function

    The random.sample() function only pick unique objects from the sequential object. If the sequential objects have repeated values, they will be treated as different sample values, and sample() function will also pick them.

    Example

    import random
    
    #list
    balls = ['red', 'blue', 'green', 'red', 'red', 'red', 'green']
    
    #pick random 4 values from balls list
    print("The four randomly picked balls are: ", random.sample(balls, k=4))

    Output

    The four randomly picked balls are: ['red', 'red', 'green', 'green']

    In the above example, you can see that the balls list has repeated items, but all of them are different objects. The sample() function only picks random unique objects, which means once the object is selected, it won't be selected again, but it does not consider the value of the objects.

    For repeated data values, we can either define it inside the sequence, as we have done in the above example or use the sample() function counts argument.

    Example

    import random
    
    #list
    balls = ['red', 'blue', 'green']
    
    #pick random 6 values from balls list
    six_random_balls = random.sample(balls, counts=[2,3,4], k=6)
    
    print("The six randomly picked balls are: ",six_random_balls )

    Output

    The six randomly picked balls are: ['green', 'blue', 'blue', 'green', 'red', 'green']

    The above random.sample(balls, counts=[2,3,4], k=6) statement is equivalent to six_random_balls =random.sample(['red','red','blue','blue','blue','green','green','green','green'], k=6) .

    Generate a Sampled list of random integers

    In random modules, we have various functions to generate random numbers or integers. Using random.randint(start, stop) we can generate a single random integer number between the start and stop, both included. And with the help of list comprehension, we can generate a list of random integers.

    Example

    import random
    
    #list of random numbers between 1000 and 9999
    random_list = [random.randint(1000, 9999) for i in range(10)]
    
    print("List of Random Numbers: ", random_list)

    Output

    List of Random Numbers: [4807, 7189, 8847, 1859, 2217, 9610, 7039, 8993, 3529, 9694]

    The random.randrange(start, stop, steps) function can also generate a random number between the start and stop intervals with a specified number of steps . We can use this function with list comprehension and generate a list of random numbers.

    Example

    import random
    
    #list of random even numbers between 1000 and 9999
    random_list = [random.randrange(1000, 9999,2) for i in range(10)]
    
    print("List of Random even Numbers: ", random_list)

    Output

    List of Random even Numbers: [3626, 9712, 4990, 9216, 4284, 3446, 1032, 7618, 5154, 2148]

    using random.randint() and random.randrange() function we can generate a list of random integers. But there is a chance that the list generated by these two functions may contain duplicate or repeated integers. To generate a list of unique random numbers, we can take the help of random.sample() function along with range() function.

    Example

    let's generate a list of 10 unique random numbers between 1000, and 9999

    import random
    
    #list of 10 random even unique numbers between 1000 and 9999
    random_list = random.sample(range(1000, 10000, 2), k=10)
    
    print("List of Random even Numbers: ", random_list)

    Output

    List of Random even Numbers: [1902, 5948, 4882, 8614, 1392, 9990, 4908, 8584, 9178, 7616]

    Generate a random sample from Python set

    The sample() function for sets has been deprecated for Python version 3.9 and newer. And it will be no longer to sample out random items from the set in the upcoming versions of Python. We will get the output with the following warning if we try to pick out random items from a set using the sample() function. DeprecationWarning: Sampling from a set deprecated since Python 3.9 and will be removed in a subsequent version.

    To pick random items from a set without any warning or error, we can convert the set into a tuple or list and select the random items.

    Example

    import random
    
    #set data
    greeting = {'Hello', 'Hi', 'Hey','whats up', ' How are you?'}
    
    random_sample = random.sample(list(greeting), k=2)
    
    print("Random Sample: ", random_sample)

    Output

    Random Sample: ['Hi', 'Hello']

    Generate a random sample from Python Dictionary

    We can not directly apply the random.sample() function on a Python dictionary, it will throw a Key error. To get the random items from the Python dictionary, we first need to convert the dictionary into a list of key, value tuple pairs using the dict.items() and list() functions.

    Example

    import random
    
    
    mobiles = {
        'iphone 13 pro max':129_900,
        'samsung galaxy z fold 3': 149_999,
        'samsung galazy s21 ultra':94_900,
        'onepluse 9 pro': 94_999,
        'asus rog 5': 49_999
        }
    
    #select two random mobiles from the prices
    random_mobiles = random.sample(list(mobiles.items()), k=2)
    
    #convert the random_mobiles list to dictionary
    print("Random Mobiles: ", dict(random_mobiles))

    Output

    Random Mobiles: {'onepluse 9 pro': 94999, 'samsung galaxy z fold 3': 149999}

    The dict.items() method return a dict_items() which is a list like object but not list. To convert it into a list, we used the list function in the above example.

    How to select the same items, again and again, using the sample function?

    Let's say you want to create a biased script that uses random.sample() function and pick the same items from the sequence. This can be done by setting the random seed. By default, to perform random tasks or generate random numbers, the operating system uses seed to initialize a pseudorandom number generator. By setting it to a constant value, we can generate the same output value from the random functions like random.sample().

    Example

    Let's generate the three same lists of 5 random numbers using random.seed() and random.sample() functions.

    import random
    
    #list from 10 to 99
    my_list = list(range(10, 100))
    
    for i in range(3):
        #set seed to 4
        random.seed(4)
        
        print(f"{i+1} random integer list: ", random.sample(my_list, k=5))

    Output

    1 random integer list: [40, 48, 23, 60, 71]
    2 random integer list: [40, 48, 23, 60, 71]
    3 random integer list: [40, 48, 23, 60, 71]

    In the output, you can see for every iteration the sample() function selects the same random items for the list. This is because we have set the seed value to a specific constant.

    How to select a random array from a multi-dimensional array in Python?

    Python's numpy library comes with a dedicated class for random data. With numpy.random.choice() function, we can pick random items from an array. The downside of the numpy.random.choice() is it only selects random items from a 1-D array.

    To select random items from a multi-dimensional array, we first have to convert it to a 1D array using ravel() method, then select the random items. At last, we can reshape the random array to the multi-dimensional array using reshape() function.

    Example

    import numpy as np
    
    md_array = np.array([
                        [1,2,3,4],
                        [5,6,7,8],
                        [9,10,11,12],
                        [13,14,15,16]])
    
    #convert the multi dimensional array to 1d array
    array = md_array.ravel()
    
    
    #select 10 random items from the array
    random_array = np.random.choice(array, 10)
    
    #convert the 1d random array to multi dimensaional array
    multi_d_array = random_array.reshape(5,2)
    
    print("The random multi-dimensional array is:\n", multi_d_array)

    Output

    The random multi-dimensional array is: 
    [[16  2]
     [13 13]
     [ 7  5]
     [ 6 16]
     [16 12]]

    Common Exception and Error while using random.sample() function

    There are two common exceptions and one common warning when we incorrectly use the random.sample() function.

    1. TypeError

    This error generally occurs when we forget to pass the 2nd argument value (the value of k ).

    Example

    random.sample(range(10))

    In the above statement, we did not pass the value of k , which is a mandatory argument, that's why Python will raise the TypeError with the following message Random.sample() missing 1 required positional argument : 'k' . We can also get the TypeError for the sample function when selecting random items from a dictionary object.

    Example

    import random
    
    #dictionary
    shortcuts = {'ctrl+c': 'copy', 'ctrl+p':'paste', 'ctrl+s':'save'}
    
    random_data = random.sample(shortcuts, k=2)
    
    print(random_data)

    In this example shortcuts is a dictionary object and sample() function does not accept dictionary as an argument and return the TypeErro with the following error message Population must be a sequence. For dicts or sets, use sorted(d).

    2. ValueError

    The random.sample() function throws the ValueError when the value of k larger than the size of the population or sequence.

    Example

    import random
    
    random_data = random.sample(range(10), k=100)
    
    print(random_data)
    

    The above example will raise the following error ValueError: Sample larger than population or is negative Because the sequence length is 10, and we are trying to get 40 items in the sample list. 3.

    DeprecationWarning

    The Python3.9 and newer versions do not support random.sample() for set data type and throw the warning. DeprecationWarning: Sampling from a set deprecated .

    Conclusion

    The random.sample(population,k) function can select k number of random unique items from a sequential object and return them as a list. The sample() function does not accept set and dictionary objects as argument values for the population. The sample() method comes very useful when we want to select random items from a list, tuple, or string. It is a perfect alternative for the random.choices() function.

    That's it about our article on the Python random.sample() function. If you like this article or have any queries related to the above examples and explanations, please share your thoughts in the comment section.

    People are also reading:

    Leave a Comment on this Post

    0 Comments