Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Password Generator using randint instead of random.choice

I tried to solve a mini quiz called Password Generator.
The idea is to print out a random char by subsetting any characters from the list by specifying how many characters, symbols, and numbers. For example, I want 3 symbols in my password, hence it should subset 3 random characters from the symbol list.

I tried 2 ways:

The first one is by using random.choice to get random elements from list variables.
It turns out successful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The second attempt was to subset the element from the list by using the index, so I used random.randint for getting me a random index from a specific range.
But my second attempt, instead of printing out different characters, gives me the same output.

Here’s the code:

import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

password = ""


for l in range(0, nr_letters):
  password += letters[random.randint(0,51)]

for s in range(0, nr_symbols):
  password += symbols[random.randint(0,8)]

for n in range(0, nr_numbers):
  password += numbers[random.randint(0,9)]

print(f"Your password is: {password}")

Here’s the samples output from the above code(assume 2chars, 2symbols, 2numbers):

**1st Run:** cc@@44
**2nd Run:** vv!!00
**3rd Run:** ee((11

I’m expecting something like aw#*14 as the output or any random outputs from the code.
Could anybody please point me to the mistake that I did? I’m new to programming and still trying to grasp the logic behind it.
Much appreciate it!
Thank you.

>Solution :

must be some optimization that affect randint. if you store it before it wont happens

import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= 2 #int(input("How many letters would you like in your password?\n")) 
nr_symbols = 2 #int(input(f"How many symbols would you like?\n"))
nr_numbers = 2 #int(input(f"How many numbers would you like?\n"))
password = ""
for l in range(0, nr_letters):
  a = random.randint(0,51)
  password += letters[random.randint(0,51)]
for s in range(0, nr_symbols):
  a=random.randint(0,8)  
  password += symbols[random.randint(0,8)]
for n in range(0, nr_numbers):
  a=random.randint(0, 9)
  password += numbers[a]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading