How to Print Random Number from Python List
Random numbers play a vital role in many applications like where the unpredictable result is needed. We may have observed a ReCaptcha pop-up when we try to log in on websites. It is to monitor the website security and it prevents our websites from any spam. That ReCaptcha uses a random number generator. and also to produce secret keys, Cryptography uses a lot of random numbers.
Another application of Random numbers are in games, gambling, Machine Learning, Statistical Analysis, etc.,
In this tutorial, we will learn how to get a random number from the list.
Example: Using the randint() Method.
This method returns an element from the specified range.
Syntax:
random.randint(start, stop)
It takes two parameters, Start and Stop.
Start and stop are required as it specifies the from which position to start and to which position to stop.
The below example shows how to get a random number from the list using the randint() method.
import random #import random module
list1=[1,2,3,4,5,6,7,8,9,10] #Intialize list1 with integer elements
list2=["python","Java","C","C++","R"] #Intialize list2 with string elements
print("Elements present in list 1 are:",list1) #print the elements in the list 1
print("Elements present in list 2 are:",list2) #print the elements in the list 2
get_random_no_1=random.randint(0, len(list1)-1) #getting random element from list 1 using the random.int() method
random_num_1 = list1[get_random_no_1]
get_random_no_2=random.randint(0, len(list2)-1) #getting random element from list 1 using the random.int() method
random_num_2= list2[get_random_no_2]
print("The random number from list 1 is:",random_num_1) #printing the random number in list 1
print("The random number from list 2 is:",random_num_2) #printing the random number in list 2
In the above example, first, we imported the random module.
Next, we initialize the list1 with integer elements and list2 with string elements.
As the randint() method takes two parameters, we gave a range from 0 to length of the list1-1.
The random number generated from the randint() method will store in the get_random_no_1 variable.
We know the list elements can be accessed by the index.
So in the next step, we used that random number as an index number, to get the element from the list.
Elements present in list 1 are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements present in list 2 are: ['python', 'Java', 'C', 'C++', 'R']
The random number from list 1 is: 9
The random number from list 2 is: python
Example: Using choice() Method
It returns a random element from the given sequence.
Syntax:
random.choice(sequence)
The sequence can be a list, tuple, set, etc.,
The below example shows how to get a random number from the list using the choice() method.
import random #import random module
list1=[1,2,3,4,5,6,7,8,9,10] #Intialize list1 with integer elements
list2=["python","Java","C","C++","R"] #Intialize list2 with string elements
print("Elements present in list 1 are:",list1) #print the elements in the list 1
print("Elements present in list 2 are:",list2) #print the elements in the list 2
get_random_no_1=random.choice(list1) #getting random element from list 1 using the random.choice() method
get_random_no_2=random.choice(list2) #getting random element from list 2 using the random.choice() method
print("The random number from list 1 is:",get_random_no_1) #printing the random number in list 1
print("The random number from list 2 is:",get_random_no_2) #printing the random number in list 2
The above example is similar to example 1.
The random.choice() method returns a random element from the given sequence.
As we were given the two sequence list1 and list2, random elements stored in the variables get_random_no_1 and get_random_no_2 respectively.
The output we get is as shown below.
Elements present in list 1 are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements present in list 2 are: ['python', 'Java', 'C', 'C++', 'R']
The random number from list 1 is: 2
The random number from list 2 is: C
Example: Using randrange() Method
This method is similar to the randint() method. This also returns a random element from the specified range.
Syntax:
random.randrange(start, stop, step)
It takes three parameters, start, stop, and step.
The below example shows how to get a random number from the list using the randint() method.
import random #import random module
list1=[1,2,3,4,5,6,7,8,9,10] #Intialize list1 with integer elements
list2=["python","Java","C","C++","R"] #Intialize list2 with string elements
print("Elements present in list 1 are:",list1) #print the elements in the list 1
print("Elements present in list 2 are:",list2) #print the elements in the list 2
get_random_no_1=random.randrange(len(list1)) #getting random element from list 1 using the random.randrange() method
random_num_1 = list1[get_random_no_1]
get_random_no_2=random.randrange(len(list2)) #getting random element from list 1 using the random.randrange() method
random_num_2= list2[get_random_no_2]
print("The random number from list 1 is:",random_num_1) #printing the random number in list 1
print("The random number from list 2 is:",random_num_2) #printing the random number in list 2
The above example is similar to example 1, random.randrange() method also returns one random element.
We used that element as the index number to get the element from the list1 and list2.
The output we get is as shown below.
Elements present in list 1 are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements present in list 2 are: ['python', 'Java', 'C', 'C++', 'R']
The random number from list 1 is: 9
The random number from list 2 is: R
Conclusion:
In this tutorial, we learned how to get a random number from the list using the random module methods.