I'd like to generate some alphanumeric passwords in Python. Some possible ways are:
import string
from random import sample, choice
chars = string.ascii_letters + string.digits
length = 8
''.join(sample(chars, length)) # first way
''.join(choice(chars) for i in range(length)) # second way
But I don't the first way because only unique chars are selected and you can't generate passwords where length > len(chars)
and I don't like the second way because we have an unused i
variable. Are there any other good options?
_
instead.random
module, it's pseudo-randomness is fine for testing but INCREDIBLY weak for this purpose. Use the built-insecrets
module instead