python tutorial

Python Random Module

Learn more about Python, one of the world’s most versatile and popular programming languages.

While writing code in Python, there is a need sometimes for developers to generate random numbers. Random numbers are very useful in building gambling games to deal a random hand to players, in cryptography and statistical sampling in Python code.

Python provides a very easy way to generate these random numbers by using the random module. These random numbers are very deterministic as they are generated using a seed value. Hence, they are not ideal for use in encryption. For a given seed, they always generate the same random numbers.

Code Example
# import random module
>>> import random
# define the seed value to be used for generating random numbers
>>> random.seed(3)
# random numbers are always a value between 0 and 1
>>> print(random.random())
0.23796462709189137
>>> print(random.random())
0.5442292252959519
>>> print(random.random())
0.36995516654807925

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required