Python Random sample()

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The random sample() Python method is used to obtain a sequence of randomly selected values from the provided input sequence. The random sample() Python method is found in the random module (a Python built-in module). The random sample() Python method takes two parameters. The first parameter is the original sequence from which the output random sequence has to be generated. The second parameter is the length of the random sample that needs to be generated. It returns a list of randomly selected values from the input sequence.

Syntax of Python Random sample()

The random Python sample method returns a list of a certain length chosen from the specified sequence of objects.

The syntax of the random sample Python is very easy.

Here, the sequence can be any sequential data type like list, set, tuple, etc. On the other hand, the length depicts the length of the sequence.

Parameters of Python Random sample()

The random Python sample method takes two parameters:

  • sequence: The sequence is any sequential collection of data (like list, set, tuple, etc.) from which the random selection of data has to be performed.

  • length: The length parameter tells the Python compiler about the length of the random sample that needs to be generated. As the length parameter tells the length of the randomly generated sequence, it must be an integer (int).

Note: The resultant randomly selected sample has to be generated from the sequence (specified as the first parameter) so the required length of the resultant sample must be less than or equal to the length of the sequence parameter.

Return Values of Python Random sample()

As we know that the random sample() Python method is used to generate a random sample of some certain length. So, the random sample() Python method returns a list of the provided length consisting of randomly selected data or values from the input sequence.

Refer to the Example section for better understanding.

Exceptions of Python Random sample()

As we have discussed above, the second parameter of the random sample() Python method is the length of the required random sequence. Now, this length needs to be smaller than or equal to the length of the actual sequence from which a random sequence has to be performed.

If the parameter length is larger than the input sequence length then the Python interpreter raises an error or exception namely ValueError.

Similarly, if the parameter length is negative or zero then the Python interpreter raises the same error (ValueError).

The error may look like this:

Now, as the length parameter specified the length of the random sample output so the length parameter must be an integer else the Python interpreter raises the TypeError exception or error.

Let us suppose that we have provided a string value in place of the integer then the error may look like this:

Example of Python Random sample()

Let us now take an example and see how the random sample() Python method works and returns a randomly selected sequence of a specified length.

Output:

What is Python Random sample()

The random sample() Python method is found in the random module. The random module is a built-in Python module.

The random sample() Python method is used to obtain a sequence of randomly selected values from the provided input sequence.

The random sample() Python method takes two parameters (both are required). The first parameter is the original sequence from which the output random sequence has to be generated. The sequence can be a list, a tuple, or a set. The second parameter is the length of the random sample that needs to be generated. The random sample() Python method returns a list of randomly selected values from the input sequence.

Some of the important points regarding the second parameter (length) of the random sample() Python method are:

  • The length parameter must be an integer (int).
  • The length parameter must be smaller than or equal to the length of the input sequence from which the output has to be generated.

Now, the random sample() Python method raises two types of error or exceptions:

  • TypeError: If the parameter length is a string or any data type other than integer then the Python interpreter raises the TypeError exception.
  • ValueError: If the parameter length is negative or greater than the length of the input sequence then the Python interpreter raises the ValueError exception.

Refer to the next section for a better understanding.

More Examples

Let us take a few examples to understand the working of the random sample() Python method for a better understanding.

Random sample() Example to Select Multiple Items from a List without Repetition

Let us suppose that we are provided with a list of programming languages and we need to randomly select 5 languages from the list.

Output in first run:

Output in second run:

As we can see that the random sample() Python method picks random values to form the input sequence in every execution.

Raise Exception

As we have seen in the explanation above, the random sample() Python method raises two types of error. Let us see both of them using an example.

When the Length is Not an Integer Value

Output:

In the above code, when we have tried to specify the length as a string value, the TypeError is generated.

When the Length is Larger than the Length of the Input Sequence.

Output:

In the above code, when we have tried to specify the length as 6 which is larger than the length of the original sequence i.e. 5, the TypeError is generated.

Conclusion

  • The random sample() Python method is used to obtain a sequence of randomly selected values from the provided input sequence.
  • The random sample() Python method is found in the random module (a Python built-in module).
  • The random sample() Python method takes two parameters. The syntax of the random sample() Python method is: random.sample(sequence, length)
  • The first parameter is the original sequence from which the output random sequence has to be generated. The second parameter is the length of the random sample that needs to be generated.
  • The random sample() Python method returns a list of randomly selected values from the input sequence.
  • If the parameter length is larger than the input sequence length then the Python interpreter raises an error or exception namely ValueError.
  • If the parameter length is negative or zero then the interpreter raises an error or exception namely ValueError.
  • If the parameter length is of any data type other than integer then the Python interpreter raises an exception namely TypeError.

See Also

To understand more about Python programming language and important Python functions, refer to: