Sitemap

Create random numbers in python

1 min readAug 24, 2021

--

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision

Get Naga Sudhir’s stories in your inbox

Join Medium for free to get updates from this writer.

In this post we will try to learn how to generate random numbers using random module in python

Create a random integer using ‘randint’

import random# create a random integer between 10 and 20
intNum = random.randint(10,20)
print('random integer between 10 and 20 = {0}'.format(intNum))

Create a random decimal value using ‘uniform’

import random# create a random real value such that 10<=realNum<=20
realNum = random.uniform(10,20)
print('random real number between 10 and 20 = {0}'.format(realNum))

Example: create a list of random numbers

import randomrealNums = []# run a loop for 15 times
for p in range(15):
realNums.append(random.randint(200,500))
print('fifteen random integers between 200 and 500 = {0}'.format(realNums))lst = [random.uniform(10,60) for k in range(4)]
print('4 random decimal numbers between 10 and 60 are {0}'.format(lst))

Video

You can the video on this post here

References

--

--

Naga Sudhir
Naga Sudhir
0 followers

I like programming for fun and adding value to people's lives