Create random numbers in python

random_numbers

Skill - Create random numbers in python

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


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 random

realNums = []

# 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


Online Interpreter

You can run these codes online at https://www.programiz.com/python-programming/online-compiler/

References


Table of Contents

Comments