UpTest > General Programming > Python Test

Test: Python Test

Question 1: Prev Next Hide answers
What is the best way to randomly select an item from list?
foo = ['a', 'b', 'c', 'd', 'e']
from random import randrange
random_index = randrange(0,len(foo))
print foo[random_index]
0 votes
foo = ['a', 'b', 'c', 'd', 'e']
from random import choice
print choice(foo)
3 votes
foo = ['a', 'b', 'c', 'd', 'e']
import random
print int(random.random() * len(foo))
0 votes
foo = ['a', 'b', 'c', 'd', 'e']
import random
print foo[int(random.random() * len(foo)+1)]
0 votes
Please login to submit your answer.