You can get one element randomly from the array by using the choice function of the random module.
However, note that if the number of elements is 0, an Exception will be raised.
>>> import random
>>> random.choice(["hoge","fuga","piyo"])
'piyo'
>>> random.choice(["hoge","fuga","piyo"])
'fuga'
>>> random.choice([])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib64/python2.7/random.py", line 275, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
IndexError: list index out of range
Recommended Posts