I want to randomly pick items from Python

Asked 2 years ago, Updated 2 years ago, 105 views

I want to randomly pick items on the list from Python.

a = [1,2,3,4,5,6,7,8,9,10] In Anything (a) sometimes 3 and sometimes 1 Is there a way to make anything come out together?

list python random

2022-09-21 23:02

1 Answers

randomUse the module

If you want to pick an item right away, choose()

import random
a = [1,2,3,4,5,6,7,8,9,10]
print random.choice(a)

If you need a random index, landrange()

import random

a = [1,2,3,4,5,6,7,8,9,10]
randomIndex = random.randrange(0,len(a))
print a[randomIndex]

Try it with me


2022-09-21 23:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.