Python Random choice() Method with Examples

Random choice() Method in Python:

The choice() method selects an element at random from the specified sequence and returns it.

The sequence could be a string, a range, a list, a tuple, or anything else.

Syntax:

random.choice(sequence)

Parameters

sequence: This is Required. It could be a list, a tuple, a range of numbers, and so on.

Examples:

Example1:

Input:

Given list = [4, 7, 9, 0, 1]

Output:

The random element from a given list =  7

Example2:

Input:

Given string = "hello"

Output:

The random character from a given string =  e

Random choice() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the random.choice() method to get a random element from a given list.
  • Store it in another variable.
  • Print a random element from a given list.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as static input and store it in a variable.
gvn_lst = [4, 7, 9, 0, 1]
# Pass the given list as an argument to the random.choice() method to get a
# random element from a given list.
# Store it in another variable.
rslt = random.choice(gvn_lst)
# Print a random element from a given list.
print("The random element from a given list = ", rslt)

Output:

The random element from a given list =  7
For Strings

Approach:

  • Import random module using the import keyword.
  • Give the string as static input and store it in a variable.
  • Pass the given string as an argument to the random.choice() method to get a random character from a given string.
  • Store it in another variable.
  • Print a random character from a given string.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the string as static input and store it in a variable.
gvn_str = "hello"
# Pass the given string as an argument to the random.choice() method to get a
# random character from a given string.
# Store it in another variable.
rslt = random.choice(gvn_str)
# Print a random character from a given string.
print("The random character from a given string = ", rslt)

Output:

The random character from a given string =  e

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import random module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the random.choice() method to get a random element from a given list.
  • Store it in another variable.
  • Print a random element from a given list.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the random.choice() method to get a
# random element from a given list.
# Store it in another variable.
rslt = random.choice(gvn_lst)
# Print a random element from a given list.
print("The random element from a given list = ", rslt)

Output:

Enter some random List Elements separated by spaces = 10 20 30 60 80
The random element from a given list = 10
For Strings

Approach:

  • Import random module using the import keyword.
  • Give the string as user input using the input() function and store it in a variable.
  • Pass the given string as an argument to the random.choice() method to get a random character from a given string.
  • Store it in another variable.
  • Print a random character from a given string.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Pass the given string as an argument to the random.choice() method to get a
# random character from a given string.
# Store it in another variable.
rslt = random.choice(gvn_str)
# Print a random character from a given string.
print("The random character from a given string = ", rslt)

Output:

Enter some random string = btechgeeks
The random character from a given string = k