import random
from random import randint
'''
random.randint()隨機生一個整數int類型,可以指定這個整數的范圍,同樣有上限和下限值,python random.randint。
random.choice()可以從任何序列,比如list列表中,選取一個隨機的元素返回,可以用於字符串、列表、元組等。
random.sample()可以從指定的序列中,隨機的截取指定長度的片斷,不作原地修改。
'''
list_one=["name","age",18]
choice_num=random.choice(list_one)
print(choice_num)
index_num=randint(1,1000)%len(list_one)
print(list_one[index_num])
print(random.sample(list_one,1)[0])
'''
利用下表隨機取值
'''
def getsomeone(listobject):
if isinstance(listobject, list) :
index_num=randint(1,1000)%len(listobject)
return listobject[index_num]
else:
return None
'''
利用random中的choice函數隨機取值
'''
def getsomeone1(listobject):
if isinstance(listobject, list):
someone=random.choice(listobject)
return someone
else:
return None
'''
random.sample()可以從指定的序列中,隨機的截取指定長度的片斷,不作原地修改。
'''
def getsomeone2(listobject):
if isinstance(listobject, list):
someone=random.sample(listobject,1)[0]
return someone
else:
return None
print(getsomeone(list_one))
print(getsomeone1(list_one))
print(getsomeone2(list_one))