Creating a Guess the Number game

Sergio Ribeiro, May 2021
The algorithm for generating random numbers is an old issue in computing. They are essential and widely used in various areas of computing, such as games, simulations, and cryptography. True random numbers, True Random Number Generators (TRNGs), are extracted from physical phenomena such as dice rolls, flipping a coin, and roulette. They are hard to obtain through algorithms. Operating systems like Unix and Linux use the dev/random function for the generation of TRNGs based on random data generated by noise from the environment or some physical phenomena on the computer, such as the electrical impulses produced by the movement of the mouse.
Due to the difficulty in obtaining TRNGs, a Pseudo-Random Number Generator (PRNG) helps. However, their use is not recommended for encryption or security applications because they are deterministic. For this reason, they are widely used in various other situations, such as gambling, statistical sampling, and computer simulation.
This article will demonstrate how to use the random module in Python to generate PRNGs and how to use it in a guessing game.
Python's Mersenne Twister PRNG
Python uses the PRNG called Mersenne Twister, which is one of the most well-known and used random number generators. Developed in 1997 by Makoto Matsumoto and Takuji Nishimura, this PRNG was designed to address many of the flaws found in previous algorithms. As we will see below, its use is straightforward and versatile.
Using the random Module in Python
As already mentioned, Python uses the random module to generate random numbers using the Mersenne Twister algorithm. This module is part of the Python 3 library, so it is not necessary to install it. To use it, just import the module:
import random
The generation of any random number can be done as follows:
import random
print(random.random())
Note that the number generated is of the float type. If you want to generate an integer between 1 and 5, you can use:
import random
print(random.randint(1, 5))
You can also specify a list of values to be randomly drawn from:
import random
print(random.choice(["yellow", "green", "blue", "white"]))
Creating the Guess the Number Game
Now that we know how to use the random method to generate random numbers, let’s create our Guess the Number game. First, we need to generate a number between 1 and 5 and store it in a variable called X. This is the number chosen by the computer! Then, the program should ask the player to guess which number the computer has in mind. The number entered by the player is saved into the variable N. In this game, the player will have three chances to guess the number. If the player guesses correctly, they win. Otherwise, the computer wins.
import random
X = random.randint(1, 5)
round = 1
print("The Computer has chosen a number between 1 and 5. Can you GUESS it?")
while round <= 3:
print("This is round", round, "out of 3")
N = int(input("What is the number? "))
if N == X:
print("Well done, YOU WIN!")
break
else:
print("Tough luck, you are wrong!")
round += 1
if round > 3:
print("Sorry, COMPUTER WINS!")
print("The number chosen by the computer was", X)
Take the opportunity to test the program and make any changes you want.
If you don’t already know Python programming language and would like to start programming in Python, take a look at Computers and Information Processing for Business: Microsoft Office 2019 and Python.