Python‎ > ‎random‎ > ‎

random.choice()

http://www.adilwaseem.com/

Introduction
Choose a random element from a non-empty sequence.

Syntax
random.choice(x)->random selection

Parameters
x=sequence

Example
import random
def python_lake(x):
    x=random.Choice(x)
    print("Hi your random element is",x)

a=input("Please enter elements for random selection: ")
python_lake(a)

>>> 
Please enter elements for random selection: 555
>>>'5'
>>>


The method selected the item from tuple.
import random
tuple=(1,3,5,7,4,6,7)
x=random.choice(tuple)
if x.__lt__(4):
                  print("Random value is more than 4")
else:
      print("Random value is: ",x)

>>> 
Random value is: 6
>>>

The method return with the sample random from the random methods.
import random
a=[random.weibullvariate(1,4),random.choice([1,3,5,7]),random.getrandbits(3)]
print(random.sample(a,1))

>>> 
[0.8132223906535521]
>>>


Comments