Let’s make an outer for loop that will loop as many times as the total number of passwords. To do this, we can simply add 61 to our random number because 97-36 = 61.įinally, we just need to cast our new integer value into a char and return it! Step 4: Create a random password by looping through the total number of passwords and looping through the length of the passwords. Lowercase letters are 97-122 in ASCII, and we currently have them in the random number interval of 36-61, so we need to convert from 36-61 to 97-122. To do this, we can simply add 55 to our random number because 65-10 = 55. Uppercase letters are 65-90 in ASCII, and we currently have them in the random number interval of 10-35, so we need to convert from 10-35 to 65-90. To do this, we can simply add 48 to our random number because 48-0 = 48. Numbers are 48-57 in ASCII, and we currently have them in the random number interval of 0-9, so we need to convert from 0-9 to 48-57. Our goal is to shift the random number interval we currently have for numbers, uppercase letters, and lowercase letters to their appropriate ASCII intervals. We can use conditionals to check if our random number corresponds to numbers, uppercase letters, and lowercase letters. If our random number is between 36 and 61 (inclusive), we can say it's a lowercase letter. If our random number is between 10 and 35 (inclusive), we can say it's an uppercase letter. In other words, if our random number is between 0 and 9 (inclusive), we can say it's a number. The 26 possible lowercase letters can be between 36 and 61 (inclusive). The 26 possible uppercase letters can be between 10 and 35 (inclusive). The 10 possible numbers can be between 0 and 9 (inclusive). Now that we have established that our random number is between 0 and 61 (inclusive), we want to think about how we will split up the values of our random number to correspond to numbers, uppercase letters, and lowercase letters. We can write (int)(Math.random() * 62) in order to get a random integer including 0 and excluding 62. There are 10 numbers, 26 lowercase letters, and 26 uppercase letters, so there are 10 + 26 + 26 = 62 possibilities of characters. The first thing we want to do is to make a random number that represents all possible characters. This information will be super important later! Step 3: Create a function that generates a random character, which can be a number, a lowercase letter, or an uppercase letter.Īccording to our ASCII table, '0' - '9' is 48-57 in ASCII, 'A' - 'Z' is 65-90 in ASCII, and 'a' - 'z' is 97-122 in ASCII. We can print out a prompt for the user to enter in the length of their passwords and store their answer in an integer, just like what we did for the total number of passwords. Hint: Make sure to use in.nextInt() to get the user input as an integer! Step 2: Ask the user how many characters long they want their passwords to be.Īccording to security experts, 12-15 characters is a strong password length! Next, we can make a Scanner variable by typing Scanner in = new Scanner(System.in).įinally, let’s print out a prompt for the user to enter in the number of passwords they want generated and store their answer in an integer. How do we do each of these steps? Step 1: Ask the user to enter the total number of random passwords they want.įirst, we need to type import at the top of our code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |