How to generate random numbers in Python Pro Preview

When installing SPIKE, several widely used libraries are also installed alongside it. One such library is the "random" library. This library allows us to generate random numbers in a range defined by us.

At every start of the program, they will be different, but sometimes they will match. They are random after all!

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #2598
  • 12 Mar 2026

In order to use this library, we must import it first. For this purpose, you must add the following line to the beginning of your program:

import random

Now we can use the commands in this library!

To generate a random number, we can use the random.randint() command. This command has two input parameters:

  1. The smallest number it can select;
  2. The largest number it can select.

Here's an example where we write a random number between 0 and 10 in a variable named "random_number":

random_number = random.randint(0, 10)