The functions of the random module in Python are sufficient for the random values of a game.
However, if you need random values for the purposes of encryption or for security reasons, you should use the functions of the Python secrets module, which provides secure random numbers and has been available since Python 3.6.
Let’s consider a simple example.
import string, secrets
li = []
for i in range(10):
li.append(secrets.randbelow(6) + 1)
print("Roll the dice ten times:", li)
tx = ""
for i in range(10):
tx += secrets.choice(string.ascii_lowercase)
print("Text with ten random lowercase letters:", tx)
The randbelow() function returns a random number between 0 and the value of the transferred parameter. The die is rolled ten times.
The choice() function returns a random value from a sequence. The sequence in this example is the ascii_lowercase string constant from the string module, which contains the lowercase letters from the American Standard Code for Information Interchange (ASCII) code. A sequence of ten random lowercase letters is determined and output.
A possible output of the program could read as follows:
Roll the dice ten times: [2, 4, 3, 4, 3, 2, 1, 1, 5, 4]
Text with ten random lowercase letters: dxhrankfme
In the following program, the choice() function is used to create a random password consisting of six characters. The password must contain at least one character from each of the following four groups: lowercase letters, uppercase letters, numbers, and special characters.
import string, secrets
special = "!#$%&()*+-/:;<=>?@[]_{|}"
print("Special characters:", special)
ch_all = string.ascii_letters + string.digits + special
while True:
tx = ""
num = [0,0,0,0]
for i in range(6):
character = secrets.choice(ch_all)
tx += character
if character in string.ascii_lowercase:
num[0] += 1
elif character in string.ascii_uppercase:
num[1] += 1
elif character in string.digits:
num[2] += 1
else:
num[3] += 1
print("Number:", num)
if 0 not in num:
break
print("Password: ", tx)
First, a character string is created that consists of all special characters that are permitted in this case. Next, a character string is put together that consists of all the characters permitted for the password (i.e., all uppercase and lowercase letters, all numbers, and all possible special characters).
The password is created in an endless loop that is exited once the password has fulfilled the conditions mentioned previously. For the password, the choice() function is used to take six characters in succession from the set of permitted characters and put them together.
We also have a list of counters, one counter for each group. The group membership is determined for each character. The corresponding counter is incremented. When no more counter shows 0, the infinite loop will be exited.
In the following possible outputs of the program, you can see how several runs of the loop were necessary:
Special characters: !#$%&()*+-/:;<=>?@[]_{|}
Number: [1, 3, 0, 2]
Number: [3, 2, 0, 1]
Number: [2, 2, 1, 1]
Password: B(kO7w
Editor’s note: This post has been adapted from a section of the book Getting Started with Python by Thomas Theis.