Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select random characters in a pythonic way? [duplicate]

Tags:

python

random

I want to generate a 10 alphanumeric character long string in python . So here is one part of selecting random index from a list of alphanumeric chars.

My plan :

set_list = ['a','b','c' ........] # all the way till I finish [a-zA-Z0-9]

index = random()    # will use python's random generator
some_char = setlist[index]

Is there a better way of choosing a character randomly ?

like image 478
Deepankar Bajpeyi Avatar asked Dec 01 '22 04:12

Deepankar Bajpeyi


1 Answers

The usual way is random.choice()

>>> import string
>>> import random
>>> random.choice(string.ascii_letters + string.digits)
'v'
like image 181
poitroae Avatar answered Dec 09 '22 20:12

poitroae