Taking a random number from list. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Taking a random number from list.

Hello world! I have a list with number from 1-100. How can i take a random number from that list.

27th Mar 2021, 4:59 PM
ΘΟΔΩΡΗΣ ΦΕΡΕΝΤΙΝΟΣ
ΘΟΔΩΡΗΣ ΦΕΡΕΝΤΙΝΟΣ - avatar
3 Answers
+ 5
import random print(random.choice(arr)) You have to use this random.choice()
27th Mar 2021, 5:01 PM
TOLUENE
TOLUENE - avatar
+ 3
you can also use the normal random.randint as well by doing it this way import random arr = [1, 2, 3] print(arr[random.randint(0, len(arr)-1)])
27th Mar 2021, 6:30 PM
Shen Bapiro
Shen Bapiro - avatar
27th Mar 2021, 5:02 PM
ΘΟΔΩΡΗΣ ΦΕΡΕΝΤΙΝΟΣ
ΘΟΔΩΡΗΣ ΦΕΡΕΝΤΙΝΟΣ - avatar