Following on from the creation of Rock, Paper, Scissors last week where we used the random module to create random plays by the computer. Today, we are going to use random again to create a randomly generated string of text, much like those of random password generators that are used by Password Management Systems.
When the program runs, it should look something like this:
To begin, we need to import the random module, from there we need to define the variable that will be used to shuffle the string of text so that it will produce something different every time.
There are several parts to this variable. It needs to start by defining what it is going to be doing, in this case, it will be shuffling the randomly generated string of text.
The ‘return ‘ ‘.join(generate) is important in the way that it is written. This will join all the characters of the string together. The middle of the ‘ ‘ needs to remain blank, as this determines what will link the tuple together when the program is run.
For example, if that line is changed to look like this: ‘#’.join(generate), the program will put a hash in between each character, as shown in the screenshot below.
Alternatively, if a space is left between those two characters, when the program is run, everything will have a space between it instead.
Now that we have defined the shuffle function within the program, it is time to create the variables that will be included within the string.
For this section, I am using two of everything. Two uppercase letters, two lowercase, two numbers and two special characters.
An ACSII code chart is needed to plot what is returned by each variable.
- Numbers 65 – 90 will print uppercase letters
- Numbers 97 – 122 will print lowercase letters
- Numbers 48 – 57 will print numbers between 0 and 9
Random symbols are harder to plot as they are dotted about between the upper and lowercase characters on the chart. That is why, the random symbols variables have two different sets of numbers applied to them.
The randint method is added to the variable so that the computer will choose a different setting every time.
When all the variables have been created, they need to be linked together, so that they can be assigned to a print statement.
It is time to call upon the shuffle function, otherwise everything listed will just print in that order.
Finally, add in a print statement and the program is complete.
It occurred to me as I was writing this, that I could reduce the size of the program but still make the outcome work in the same way. To do this, I removed some of the excess variables from the program, as shown in the screenshot below.
This in turn, made listing the variables cleaner and less plus symbols to combine them all together. In addition to reducing the number of variables, I multiplied the password by two, which increased the length of the string but still kept it random and didn’t just duplicate the same string.
I hope that you have enjoyed reading through and perhaps trying this out for yourself. As an additional challenge, why not try building on this by editing the text further still, or by getting this to display on a GUI pop-up.