How to Get Random Numbers and Elements in Python| Python Tutorial

Python randomness with proper examples

Sanjay Priyadarshi
5 min readNov 15, 2022

--

Photo by Growtika Developer Marketing on Unsplash

With the help of the random module, you can easily work with random numbers in python.

If you want to generate a new random number or you want to get a random number from a given list.

You can use random module methods to do that.

Here we will discuss in detail how you can work with random numbers in python.

Generate random numbers

To generate a random number, you must first import the random module.

Without the help of the random module, you cannot work with random numbers in python.

How to generate a random number between 0 and 1

If you want to generate a random number between the range of 0 and 1, you can use the random() method to do so.

The random() method is part of a random module.

Let’s see this with an example.

import random 

a = random.random()

print(a)


#Result:
0.2196286945099909

How to generate a random number in a specific range

--

--