My question is: how to generate a random password with a length that varies from (8 to 12 characters) each time generated.
This is my code:
import string
import random
def randompassword():
chars=string.ascii_uppercase + string.ascii_lowercase + string.digits
size=8
return ''.join(random.choice(chars) for x in range(size,12))
print(randompassword())
You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified. See also chapter Running a generator for more information.
size = 8; range(size, 12)
always returns array [8,9,10,11]
, so you always get a password of length 4.
Instead, determine the size of this particular password using randint
ahead of time:
import string
import random
def randompassword():
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
size = random.randint(8, 12)
return ''.join(random.choice(chars) for x in range(size))
There are nuances to this question and certain solutions may cause password weakness
problems. Consider the following nuances along with the solution alternative.
string.printable
, but this isn't a good idea since that contains whitespace characters. While they are not strictly illegal for passwords, you cannot easily see them and therefore cannot differentiate, say, a tab from several spaces (and so on). Below I only employ lowercase letters & uppercase letters, digits and punctuation characters.random.choices()
is used (as in other answers), one should also employ it's weights=
and cum_weights=
options, to eliminate aforementioned biases and even-out the distribution.secrets
module rather that its random
module for this use case. From their documentation on random:Warning: The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets module.
Here is one functionally-oriented solution using Python-3. It uses secrets.choice()
only. It doesn't totally solve the random problem (other nuances remain), but it does improve selection-distribution to reduces bias:
>>> import string, secrets
>>> char_classes = (string.ascii_lowercase,
string.ascii_uppercase,
string.digits,
string.punctuation)
>>> size = lambda: secrets.choice(range(8,13)) # Chooses a password length.
>>> char = lambda: secrets.choice(secrets.choice(char_classes)) # Chooses one character, uniformly selected from each of the included character classes.
>>> pw = lambda: ''.join([char() for _ in range(size())]) # Generates the variable-length password.
DEMO: Generate 10 variable-length password-strings using characters uniformly selected from each of our character classes:
>>> for i in range(1,11):
>>> p = pw()
>>> print('%i) %i chars :: %s' % (i,len(p),p))
1) 11 chars :: IwWNEAUmnJt
2) 10 chars :: ;N/'tO6RTv
3) 8 chars :: l=5.2CDh
4) 10 chars :: V0=I+A`t2Q
5) 12 chars :: PQm8:f,#56"9
6) 10 chars :: KOdx9~%r;F
7) 11 chars :: <?67U8}3>F{
8) 11 chars :: G$5y~3fE7o*
9) 10 chars :: 70,|=Rexwn
10) 8 chars :: &31P^@cU
Finally, while we used the secrets
module here, something similar could be done using numpy
and numpy.random
. I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With