Want to build a number guessing game and not sure how to generate a random number? Or a program that generates a random sequence of words or colors? Well, generating random numbers in Python with random module is easy.
Python has a standard module, random, to generate random numbers and sequences. The random module has many functions which help us to generate random numbers. Let’s see how to generate random numbers in python with a few of the functions from the random module.
Generating random integers
In role-playing games such as the Turtle Race game or Rock Paper Scissors Shoot, we need random integers. Sophia a student at Skoolofcode, built her Guess the Number game by generating random integers. Simulating the Roll, a dice program also needs a random integer.
This function returns random integer between the range you specify. If you don’t specify the start value, it will be set to 0. The stop value specifies the end of the range. Step specifies the increment to added to the previous number. Default step value is 1.
Let’s see a few examples-
This prints a random number between 0 to 10 (exclusive).
This will print either 1 or 2 or 3 or 4.
This will print any random integer from numbers 6,5,4,3,2,1.
Will print any of the random numbers from 12 or 14 or 16 or 18 or 20.
This function generates a random integer between the range specified. Both the start and the stop values are mandatory and the stop value is inclusive.
This prints a random number between 5 to 10 (inclusive).
Generating random floating-point numbers
We can also generate random floating-point numbers.
This function returns a random real number between the range specified (inclusive). Both the start and the stop values are mandatory.
Will print a random float number between 1 and 10 (inclusive).
This function returns a random floating-point number between 0 to 1 (inclusive).
Generating random sequences
We can also use the random module to add randomness to any sequence. For example, shuffling a list of words or choosing a random color from a tuple of colors. Let’s see how.
This function returns random elements from the sequence. This function is most often used to get random words in-game building projects. See how Ariv-a student at Skoolofcode, used this function in his Hangman Game project.
words = [‘cat’, ‘dog’, ‘pig’]
Will print a random word from the list of words.
colors=(“black”, “blue”, “orange”, “red”, “pink”, “white”, “yellow”, “green”)
The variable chosencolor will hold a random color (string) from the tuple of colors.
Will print a random character from the string “Skoolofcode”.
This shuffles the sequence elements in random order.
words = [‘cat’, ‘dog’, ‘pig’]
This will shuffle words into any random order.
This function returns random elements from the sequence (up to k elements). The parameter k is optional. If not specified, it will return all the available elements from the sequence in random order.
colors = (“black”, “blue”, “orange”, “red”, “pink”, “white”, “yellow”, “green”)
The function call above will print 4 random colors from the tuple of color values.
This will print 5 random words from the list of words.
Initialising the random number generator
Sometimes we may want to generate a particular sequence or number with some randomness. Let’s see how to do that in Python.
Seeds play an important role in Python’s pseudorandom number generator algorithm. This function sets an initial seed value for the pseudo-random number generator. If you don’t pass any parameter to this, the seed will be set to the current timestamp or os.getpid().
Sets the seed to current timestamp.
This seeds the random number generator using 10 as the seed, so the pseudo-random number will always be the same.
This will always print a character ‘d’ from string Skoolofcode.
That’s it for this article. I hope you learned something new. But that’s not all. Python is an amazing programming tutorial online! Like any other high-level programming language, Python has all the features that make it a powerful language for game programming. There are many other functions in the random module. Refer to the official documentation of the random module in Python to learn more. Happy coding