-2

I have written a Python program to generate password. There is a glitch because it is not properly shuffled. Please suggest some methods to do it. Also suggest better methods for the same.

import random 
import array

digits = ['0','1','2','3','4','5','6','7','8','9']
lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
uppercase = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
Symbols = ['!','@','#','$','%','*','&']

mixture = digits + lowercase + uppercase + Symbols

random_digit = random.choice(digits)
random_lowercase = random.choice(lowercase)
random_uppercase = random.choice(uppercase)
random_symbol = random.choice(Symbols) 

Password = random_digit + random_lowercase + random_uppercase + random_symbol

length = random.randint(8,12)

for x in range (length) :
    Password = Password + random.choice(mixture) 

print(Password)
Peter O.
  • 32,158
  • 14
  • 82
  • 96

2 Answers2

2

To generate your original mixture you can leverage the character sets available in string. Also note you don't need a list of individual chars, instead a single str is also an iterable sequence for this purpose.

>>> from string import ascii_letters as letters
>>> from string import digits as digits
>>> symbols = '!@#$%*&'
>>> mixture = digits + letters + symbols
>>> mixture
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%*&'

Then to generate some example passwords you can use random.choices with the k argument specifying the length to sample.

>>> import random
>>> ''.join(random.choices(mixture, k=random.randint(8,12)))
'tF6iUwki1Ir'
>>> ''.join(random.choices(mixture, k=random.randint(8,12)))
'HpPJI6@m&'
>>> ''.join(random.choices(mixture, k=random.randint(8,12)))
'$KKzoiD&'

Note that random.choices allows duplicate characters, if you want them all to be unique then random.sample would be preferred.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    You should be using `secrets` instead of the `random` for this purpose. *In particularly, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.* https://docs.python.org/3/library/secrets.html#recipes-and-best-practices – sshow Dec 29 '20 at 12:57
  • @Cory It is showing up module random has no attributes 'choices'. – Shubham Kumar Dec 29 '20 at 13:06
  • @sshow Somebody might have programmed that too. So how it is that one will perform better than other? – Shubham Kumar Dec 29 '20 at 13:09
  • 1
    @ShubhamKumar What version of Python are you using? `random.choices` is new to Python 3.6. Otherwise can do something like `''.join(random.choice(mixture) for _ in range(random.randint(8.12)))` – Cory Kramer Dec 29 '20 at 13:11
0

I am just working on the method suggested by Cory Kramer.

from string import ascii_letters as letters
from string import digits as digits

symbols = '!@#$%&*'

mixture = digits + letters + symbols

import random
print('' .join(random.choice(mixture) for x in range (random.randint(8,12))))