0% found this document useful (0 votes)
13 views

Random Module

Uploaded by

aaryanrkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Random Module

Uploaded by

aaryanrkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bodhi Sattva Institute

Bodhi Sattva Institute


Random Module
Random in Python :
When ever there is a situation where we need to generate random numbers in coding
of python, then python allow us to generate random numbers by using
module RANDOM in Python.
Following are the situations where we need to generate random numbers in Python
1. To generate the scratch card of online Lottery.
2. To generate captcha code.
3. To generate OTP (One Time Password)
4. Computer games like LUDO (where need to generate random number between
1 to 6)

Functions in random Module


There are many functions in random module in python. Let we start

randrange( ) :
This function generates a random number between the lower and upper limit
(including lower limit and excluding upper limit). for example the following code will
generate a random number less than 20 :
import random
a=random.randrange(20)
print(a)
The above code will generate any number from 0 to 19.

NOTE :
1. By default the lower limit of randrange function is 0.
2. randrange( ) always generate a number one less than the higher limit.

Q1. Write the code to generate a number less than 10.


Ans.
import random
a=random.randrange(10)
print(a)

Q2. Write the code to generate a random number between 7 and 17 (both inclusive)
Ans.
import random
a=random.randrange(7, 18)
print(a)

Q3. Write the code which always generate 0 number by using randrange( ).
Ans.
import random
a=random.randrange(1)
print(a)

Prepared By: Himanshu Goyal(PGT CS)


Bodhi Sattva Institute

Q4. Write the code to select random names from the given list of names.
import random
name=["Amit","Sumit","Naina","Suman","Parth"]
rd=random.randrange(______________)
print(name[rd])

What minimum number should we fill in the above blank so that it will print any
name from the given list. (Answer is 5)

random():
This function generates a random floating point number from 0 to 1(including 0 and
excluding 1) like 0.15895645215423562.

Syntax is :
random.random()

random( ) function takes no argument.


NOTE : It always generate floating point number between 0 and 1(including 0 and
excluding 1)

Q5. Write code to generate any numbers between 10 and 15(including both)
import random
print(int(random.random( )*6+10))

Q6. Write the code to generate random three digit number using random in python.
import random
print(int(random.random( )*900+100))

Q7. Write the code to generate random two digit number using random in python.
import random
print(int(random.random( )*90+10))
randint() :
This function generates a random number between the lower and upper
limit (including both the limits).
Syntax of randint() is
a = random.randint(L,U) # where L is the lower limit and U is the upper limit.
The value of a will be in the range ( L<=a<=U)

Q8. Write a program to generate a random number between 20 and 30(including both)
import random
print(random.randint(20,30))

Prepared By: Himanshu Goyal(PGT CS)


Bodhi Sattva Institute

Q9. Write a program which adds any random five even numbers in a list that falls
between the highest and the lowest number.(Both highest and lowest numbers are
accepted from the user.)
import random
L=[]
h = int(input("Enter the highest limit"))
l=int(input("Enter the lowest limit"))
while(i):
a=random.randrange(l+1,h) #we are not including upper limit and lower limit
if(a%2==0):
L.append(a)
if(len(L)==5):
break
print(L)

Quiz time
Q1. Write the output of the following code :

import random
for i in range(random.randint(2,2)):
print(i)

0
1

1
2

0
1
2

Generate different number

Prepared By: Himanshu Goyal(PGT CS)


Bodhi Sattva Institute

Q2. Write the output of the following code :

import random
for i in range(random.randint(2,3)):
print("A")

A
A

A
A
A

A
A

OR

A
A
A

ERROR
Q3. What is the minimum and maximum value of 'A' in the following statement..
import random
A=random.randint(2,30)
print(A)
3 and 30

2 and 30

2 and 29

Prepared By: Himanshu Goyal(PGT CS)


Bodhi Sattva Institute

Q4. Write the output of the following code :


import random
A=random.randrange(20)
print(A)
Any number between 0 to 19 (including both)

Any number between 0 to 20 (including both)

Any number between 1 to 20 (including both)

Q5. Write the output of the following code :


import random
print(int(random.random()))
Any random number less than 1 and greater than 0

Always generate 0

Always generate 1

Shows Error

Q6. Write the output of the following code :


import random
print(int(random.random()*5))
Always generate 0

Generate any number between 0 to 4 (including both)

Generate any number between 0 to 5 (including both)

Q7. Which of the following subject will never printed in following code?

import random
L=["English","Hindi","Math","Science","SST","CS","IP"]
for i in range(random.randint(1,6)):
print(L[i])
CS and IP both

English

Prepared By: Himanshu Goyal(PGT CS)


Bodhi Sattva Institute

IP

English and Hindi both

Q8. What is the minimum and maximum value of 'v' in the following statement.
import random
v=random.randrange(20)-3
print(v)
0,19

0,17

-3,16

-3,17

Q9. Which of the following method is not included in random module?

Shuffle( )

randrange( )

randomrange( )

uniform( )

Q10. A = random.______([1,2,3,4,5])
Value of A can be 1 to 5(including both)
shuffle

choice

uniform

none of these

Prepared By: Himanshu Goyal(PGT CS)

You might also like