How to Generate Random Numbers within Given Range in Python


In this article, we will get the methods to generate random numbers in a given range with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import random

# Get random number in 0-1
print('Random number within 0-1:')
print(random.random())

# Get random number in a range
print('\nRandom number within 10-1000:')
print(random.uniform(10,1000))

# Another way to get a random number in a range
print('\nRandom number within 10-1000:')
print(10 + random.random()*990)

Output:

Random number within 0-1:
0.6107800497572815

Random number within 10-1000:
847.4527240646199

Random number within 10-1000:
245.28270553906327
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement