choice() in python by R4R Team

- choice() is a python function which is predefined in the random library.
- choice() function take the sequence of number and return the one random value from the sequence.
- this is work on sequence like list, tuple and set

Example-
list is 1,2,3,4,5,6,7
choice will return any number out of this list.

program-

import random
print("Enter number in list")
l=list(map(int,input().split()))
print("How many random number you want from this list")
n=int(input())
for i in range(n):
print("Random number from list is - "+str(random.choice(l)))


output-

Enter number in list
2 4 6 8 1 5 9 0
How many random number you want from this list
5
Random number from list is - 1
Random number from list is - 9
Random number from list is - 9
Random number from list is - 9
Random number from list is - 2

Enter number in list
23 45 67 12 41 76 23
How many random number you want from this list
4
Random number from list is - 45
Random number from list is - 12
Random number from list is - 67
Random number from list is - 23


-In this program,firstly we import the random library by import random, then we take input of element of list and we want to get he random number from this list so that we pass this list as the argument of choice() function then choice will return any random number from the list


Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!