Create Random Password Generator in Python

Raghav Agrawal
4 min readMar 26, 2022

we are going to develop a simple python application known as Random Password Generator or also known as OTP generator using a simple python inbuilt module named random.

In our previous articles, we have used random modules in developing a small project using the random module as Snake — Water and Gun Game. Please look at that project too which will help you to develop a basic understanding of the random module that will help in this project.

let’s get started with this activity. First, it’s essential to understand the task what actually we have to done.

Understanding the task

we have to display a password of the desired length specified by the user. Input the number of uppercase, lowercase, digits, and special character the user requires in the password. After taking all these inputs handle some common exceptions which may occur to select the random desired numbers from the ASCII list. Join the obtained characters and digits and shuffle them to look like a One-time password and display it on screen.

Now, we have understood the problem statement so let’s make our hands dirty by writing some code in a pythonic way.

IMPLEMENTATION

Step-1) Import a Random Module first and take specified Inputs from the user.

we have taken the inputs under a while loop to handle one exception is if the desired number of characters is more than the required total length of the password given by the user so we will raise an error if such a case occurs. So, the below code continues in a while loop.

  if num_char < num_upper + num_lower + num_digit + num_symbol:
print("Total char does not match your demand!\n")
else:
break

If the inputs are correct, we will come out of the loop. And if num_char is greater than the desired number of inputs then an error will not be there because we will randomly generate the char if remaining to fulfill the requested length of the password.

Step-2) Take the list of requested characters from ASCII List

The below code will be outside the While loop because we got the correct inputs from a user and now we have to create a list from where we will select the required random of specific characters.

upper_list = [chr(i) for i in range(65, 65+26)]lower_list = [chr(i) for i in range(95, 95+26)]digit_list = [str(i) for i in range(0, 10)]symbol_list = [chr(i) for i in range(32, 48)]symbol_list += [chr(i) for i in range(58, 65)]symbol_list += [chr(i) for i in range(91, 97)]symbol_list += [chr(i) for i in range(123, 127)]

Here, we have taken the list of upper, lower digit, and special character lists. special characters are present at a different location so we have to grab them in a list using the append operator or you can also use the append function of the list.

Step-3) Randomly chooses the required characters from a list.

Implement a function where we will be passing the ASCII list which we created and the number of particular characters we required and get the randomly chosen characters from the list in return in form of a list.

Step-4) Call the function and get the requested characters randomly

upper_char = getChar(upper_list, num_upper) 
lower_char = getChar(lower_list, num_lower)
digit_char = getChar(digit_list, num_digit)
symbol_char = getChar(symbol_list, symbol_upper)

Step-5) Control the exception of remaining characters

If the user has inputted fewer characters and the length of a password is more, then we have to fill in the remaining character to generate a password of the requested length.

num_unfilled_char = num_char - num_upper - num_lower - num_digit - num_symbol  
whole_list = upper_list + lower_list + digit_list + symbol_list remaining_char = getChar(whole_list, num_unfilled_char)

Step-6) Join and shuffle the Password.

Now, we have our password with us, only we have to combine and shuffle it randomly to generate the OTP

password = upper_char + lower_char + digit_char + symbol_char + remaining_char   
random.shuffle(password)
password = "".join(password)
print("Your OTP: ", password)

Congratulation! we have made our application. if you run it OTP will be generated on your screen.

when you run the code you will have the output generated like this

Conclusion

We are capable to understand and use a random module in a very nice way. We have implemented 2 small projects using a random module. I hope you are now able to use it to solve problem statements related to it and enjoyed the complete tutorial. I have an exercise to implement a number-guessing game using a random module between user and system. And paste the code or upload code at GitHub and paste the link in the comment section below. I will provide a solution to the same in the next article.

Build a random guessing number game between ranges and guide a user to guess it properly. display the number of trials he used. Upload your code on GitHub and paste the link in the comment section below.

👉 Complete Project Source Code is available at Github.

👉 The blog post is originally published by crazy-techie. For more such amazing projects please visit crazy-techie.

keep Learning, Happy Learning

Originally published at https://rgcrazytech.blogspot.com.

--

--