Random Module in Python
Theory/Concept-Based Questions
1. Which module in Python is used to generate Random numbers?
Answer: random module.
2. What is a random module? Write the statement to import a random module?
Answer: Random module is a random-number generator, which generates a random
number by chance i.e. randomly.
import random # to import a random module
3. Write the name of the functions available in the random module.
Answer: random() , randint(), uniform( ), randrange()
4. Explain the uses of random(), randint(), uniform( ), randrange()
Answer: Uses of random module functions are
Function Name Uses / Description Example
returns a random floating point
>>> import random
number N in the range(0.0, 1.0). The
random( ) >>> [Link]()
generated number is greater than or
0.896986986196
equal to 0.0 and less than 1.0.
>>>[Link](1,4)
4
returns a random integer N in the >>>[Link](1,4)
randint(a,b)
range(a,b) i.e. a <=N <= b. 2
>>>[Link](1,4)
1
returns a random floating point number >>>[Link](11,55)
uniform(a, b)
N in the range(a,b) i.e. a <=N <= b. 32.42694236141252
>>>[Link](5)
randrange(stop)
it returns a randomly selected element 4
randrange(start,
from range(start, stop, step) >>>[Link](5,15,4)
stop, step)
9
Application/Output Based Question Answer
5. Write the statement to generate a random floating point number between 25 to
50.
Answer: import random
number1 = [Link]( ) * (50-25) + 25 #method 1
number2 = [Link](25, 50) #method 2
print(number1)
print(number2)
6. Write the statement to generate a random integer number between 25 to 50.
Answer: import random
number1 = int( [Link]( ) * (50-25) + 25 ) #method 1
number2 = [Link](25, 50) #method 2
print(number1)
print(number2)
7. What could be the minimum possible and maximum possible numbers by the
following code?
1 import random
2 print([Link](15, 30) - 7)
Answer: Minimum possible number = 8
Maximum possible number = 23
8. Consider the following code:
1 import random
2 print( int(20 + [Link]() * 5), end = ' ') # (i)
3 print( int(20 + [Link]() * 5), end = ' ') # (ii)
4 print( int(20 + [Link]() * 5), end = ' ') # (iii)
5
print( int(20 + [Link]() * 5)) # (iv)
Find the suggested output options (i) to (iv). Also, write the least value and highest
value that can be generated.
(a) 20 22 24 25 (b) 22 23 24 25
(c) 23 24 23 24 (d) 21 21 21 21
Answer: Options (c) and (d) are possible.
Least Value = 20
Highest Value = 24
9. Consider the following code:
1 import random
2 print( 100 + [Link](5,10), end = ' ')
3 print( 100 + [Link](5,10), end = ' ')
4 print( 100 + [Link](5,10), end = ' ')
5 print( 100 + [Link](5,10))
Find the suggested output options (i) to (iv). Also, write the least value and highest
value that can be generated.
(a) 102 105 104 105 (b) 110 103 104 105
(c) 105 107 105 110 (d) 110 105 105 110
Answer: Options (c) and (d) are possible.
Least Value = 105
Highest Value = 110
10. Consider the following code:
1 import random
2 r = [Link] (100, 999, 5) #line 2
3 print(r, end = ' ')
4 r = [Link](100, 999, 5)
5 print(r, end = ' ')
6 r = [Link] (100, 999, 5)
7 print(r)
Find the suggested output options (i) to (iv). what can be the maximum and
minimum number generated by line 2?
(a) 655, 705, 220 (b) 380, 382, 505
(c) 100, 500, 999 (d) 345, 650, 110
Answer: Options (a) and (d) are possible.
Minimum Number = 100
Maximum Number = 995
11. Consider the following code:
1 import random
2 r = [Link] (50, 100) - 50 #line 2
3 print(r, end = ' ')
4 r = [Link] (50, 100) - 50
5 print(r, end = ' ')
Find the suggested output options (i) to (iv). what can be the maximum and
minimum number generated by line 2?
(a) 49 55 (b) 12 9
(c) 1 39 (d) 3 98
Answer: Options (b) and (c) are possible.
Minimum Number = 0
Maximum Number = 50
12. Consider the following code:
1 import random
2 AR = [20, 30, 40, 50, 60, 70]
3 Lower = [Link](1, 3)
4 Upper= [Link](2, 4)
5 for K in range (Lower, Upper+1):
6 print(AR[K], end=”#”)
What possible output(s) are expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the Minimum and
Maximum values that can be assigned to each of the variables Lower and Upper
(a) 10#40#70# (b) 30#40#50#
(c) 50#60#70# (d) 40#50#70#
Answer: Options (b) and (c) are possible.
Lower : – Minimum Value = 1, Maximum Value = 3
Upper: – Minimum VAlue = 2, Maximum Value = 4
13. Consider the following code:
1 import random
2
3 STRING="CBSEONLINE"
4 NUMBER = [Link] (0, 3)
5 N=9
6 while STRING[N] != "L":
7 print (STRING[N] + STRING[NUMBER] + "#", end = " ")
8
NUMBER = NUMBER +1
9
N=N-1
What possible output(s) are expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the Minimum and
Maximum values that can be assigned to the variable Number.
(a) ES#NE#IO# (b)LE#NO#ON#
(c) NS#IE#LO# (d) EC#NB#IS#
Answer: Options (a) and (d) are possible.
Number : – Minimum Value = 0, Maximum Value = 3
14. What will be the output of the following Python code?
1 import random
2
3 List=["Delhi","Mumbai","Chennai","Kolkata"]
4
5 for y in range(4):
6 x = [Link](1,3)
7 print(List[x],end="#")
a. Delhi#Mumbai#Chennai#Kolkata# b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi# d. Mumbai# Mumbai #Chennai # Mumbai
ANSWER:
b. Mumbai#Chennai#Kolkata#Mumbai#
15. What possible output(s) is/are expected to be displayed on the screen at the
time of execution of the program from the following code?
Also, specify the maximum and minimum value that can be assigned to the
variable R when K is assigned value as 2. [CBSE Board 2021]
1 import random
2 Signal = [ 'Stop', 'Wait', 'Go' ]
3 for K in range(2, 0, -1):
4 R = randrange(K)
5 print (Signal[R], end = ' # ')
(a) Stop # Wait # Go # (b) Wait # Stop #
(c) Go # Wait # (d) Go # Stop #
ANSWER:
(b) Wait # Stop #
When K is assigned value as 2
Maximum value of R = 1
Minimum value of R = 0
16. What is possible output(s) expected to be displayed on screen at the time of
execution of the program from the following code ? Also specify the minimum and
maximum values that can be assigned to the variable End. [CBSE Board 2020]
1 import random
2 Colours = ["VIOLET","INDIGO","BLUE","GREEN", "YELLOW","ORANGE","RED"]
3 End = randrange(2)+3
4 Begin = randrange(End)+1
5 for i in range(Begin,End):
6 print(Colours[i],end="&")
(i) INDIGO&BLUE&GREEN& (ii) VIOLET&INDIGO&BLUE&
(iii) BLUE&GREEN&YELLOW& (iv) GREEN&YELLOW&ORANGE&
ANSWER:
(i) INDIGO&BLUE&GREEN&
(iii) BLUE&GREEN&YELLOW&
Minimum Value of End = 3
Maximum value of End = 4
17. What is possible output(s) expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the minimum
values that can be assigned to the variable BEGIN and LAST. [CBSE Board 2019]
1 import random
2
3 VALUES=[10,20,30,40,50,60,70,80]
4
5 BEGIN = [Link](1, 3)
6 LAST = [Link](BEGIN, 4)
7
8 for I in range(BEGIN, LAST+1):
9 print(VALUES[I], "-",)
(i) 30 – 40 – 50 – (ii) 10 – 20 – 30 – 40 –
(iii) 30 – 40 – 50 – 60 – (iv) 30 – 40 – 50 – 60 – 70 –
ANSWER:
(i) 30 - 40 - 50 -
Minimum Value of BEGIN = 1
Minimum value of LAST = 1
18. What is possible output(s) expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the minimum
values that can be assigned to the variable Start and End. [CBSE Board 2019
Compartment]
1
2 import random
3 VAL=[80, 70, 60, 50, 40, 30, 20, 10]
4 Start = [Link](1, 3)
5 End = [Link](Start, 4)
6 for I in range(Start, End+1):
7 print(VAL[I], "*", )
(i) 40 * 30 * 20 * 10 * (ii) 70 * 60 * 50 * 40 * 30 *
(iii) 50 * 40 * 30 * (iv) 60 * 50 * 40 *
ANSWER:
(iv) 60 * 50 * 40 *
Minimum Value of Start = 1
Minimum value of End = 1
19. What is possible output(s) expected to be displayed on the screen at the time of
execution of the program from the following code? Also, specify the maximum
values that can be assigned to the variable BEGIN and LAST. [CBSE Board 2018]
1 import random
2
3 POINTS = [30,50,20,40,45];
4
5 BEGIN = [Link](1, 3)
6 LAST = [Link](2, 4)
7
8 for C in range(BEGIN, LAST+1):
9 print(POINTS[C], "#",)
(i) 20#50#30# (ii) 20#40#45#
(iii) 50#20#40# (iv) 30#50#20#
ANSWER:
(iii) 50#20#40#
Maximum Value of BEGIN = 3
Maximum value of LAST = 4
20. What are the possible output(s) executed from the following code? Also specify
the maximum and minimum values that can be assigned to the variable NUM.
1 import random
2
3 NAV = ["LEFT", "FRONT", "RIGHT", "BACK"]
4
5 NUM = [Link](1,3)
6 NAVG = ""
7
8 for C in range (NUM, 1, -1):
9 NAVG = NAVG + NAV[C]
10
11 print(NAVG)
(i) BACKRIGHT (ii) BACKRIGHTFRONT
(iii) BACK (iv) LEFTFRONTRIGHT
ANSWER:
(i) BACKRIGHT
Maximum Value of NUM = 3
Minimum value of NUM = 1
21. What are the possible output(s) executed from the following code? Also specify
the maximum and minimum values that can be assigned to the variable NUM.
1 import random
2
3 NAV = ["LEFT", "FRONT", "RIGHT", "BACK"]
4
5 NUM = [Link](1, 3)
6 NAVG = ""
7
8 for C in range (NUM, 1, -1):
9 NAVG = NAVG + NAV[C]
10
11 print(NAVG)
(i) BACKRIGHT (ii) BACKRIGHTFRONT
(iii) BACK (iv) LEFTFRONTRIGHT
ANSWER:
(i) BACKRIGHT
Maximum Value of NUM = 3
Minimum value of NUM = 1
22. What possible output(s) are expected to be displayed on the screen at the time
of execution of the following program : [CBSE 2023]
1 import random
2
3 M- (5, 10, 15, 20, 25, 30]
4 for i in range (1, 3) :
5 first = random. randint (2, 5) - 1
6 sec = random. randint (3, 6) - 2
7 third=random. randint (1, 4 )
8
9 print (M[first] , M[ sec], M[third] , sep = "#")
(i) 10#25#15 (ii) 5# 25#20
20#25#25 25#20#15
(iii) 30#20#20 (iv) 10#15#25#
20#25#25 15#20#10#
ANSWER:
(i) 10#25#15
20#25#25
[Link] are the possible outcome(s) from the following code?
import random
PICK=[Link] (1,3)
CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY:
for J in range (0, PICK):
print (I, end = "")
print ()
(i) (ii) (iii) (iv)
DELHIDELHI DELHI DELHI DELHI
MUMBAIMUMBAI DELHIMUMBAI MUMBAI MUMBAIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATAKOLKATA KOLKATA
Ans. Option (i) and (iii) are possible
[Link] are the possible outcome(s) from the following code? Also, specify the
maximum and minimum values that can be assigned to variable SEL.
import random
SEL=random. randint (1, 3)
ANIMAL = ["DEER", "Monkey", "COW", "Kangaroo"]
for A in ANIMAL:
for AA in range (0, SEL):
print (A, end ="")
print ()
(i) (ii) (iii) (iv)
DEERDEER DEER DEER DEER
MONKEYMONKEY DELHIMONKEY MONKEY MONKEYMONKEY
COWCOW DELHIMONKEYCOW COW KANGAROOKANGAROOKANGAROO
KANGAROOKANGAROO KANGAROO
Ans. Maximum value of SEL is 3 and minimum is 1
(iv) is the correct option.
[Link] are the possible outcome(s) executed from the following code ?
import random
def main():
p = "MY PROGRAM"
i=0
while p[i] != "R":
l = [Link](0,3) + 5
print (p[l],end ="_")
i += 1
main()
(i) (ii) (iii) (iv)
M_M_Y_P R_G_A_G G_G_R_O O_G_G_A
Ans. (i) will not be printed
[Link] is the incorrect outcome executed from the following code? Also, specify
the maximum and minimum values that can be assigned to variable
x=3
N = [Link] (1, x)
for i in range (N):
print (i, "#", i + 1)
(i) (ii) (iii) (iv)
0#1 0#1 0#1 0#1
1#2 1#2 1#2
2#3 2#3
3#4
Ans. (iii) will not be printed
The maximum value of N is 3 and minimum is 1
[Link] is the incorrect outcome executed from the following code?
import random
movie_list = ['The Jungle Book', 'Finding Nemo', 'Harry Potter', 'The Incredibles', 'Frozen']
movie1 = [Link](movie_list)
movie2 = [Link](movie_list)
print ( movie1 ," or " ,movie2)
1. Finding Nemo or Harry Potter
2. Harry Potter or Toy Story
3. Frozen or The Jungle Book
4. Toy Story or Spider Man
Ans. (ii) and (iv) are incorrect option.
[Link] are the possible outcome(s) executed from the following code ?
import random
color_list = ["Pink", "Red", "Sky blue", "Yellow", "Green"]
[Link](color_list)
print ( color_list )
1. [‘Sky blue’, ‘Green’, ‘Yellow’, ‘Pink’, ‘Red’]
2. [‘Blue’, ‘Green’, ‘Pink’, ’Brown’, ‘Red’, ‘Black’]
3. [Yellow, Sky blue, Green, Pink, Red]
4. (‘Yellow’, ‘Red’, ‘Sky blue’, ‘Green’, ‘Pink’)
Ans. (i) , (iv) is a correct output
[Link] is the incorrect outcome(s) executed from the following code?
import random
list=[14, 62, 30, 57, 91]
str= ('Simply')
print ( [Link](list),"and" , [Link](str) )
1. 57 and i
2. 62 and S
3. 20 and p
4. 30 and M
Ans. (iii) and (iv) are incorrect options
[Link] are the possible outcome(s) executed from the following code ? Also
specify the maximum and minimum values that can be assigned to variable
PICKER.
import random
N=3
PICKER = [Link] (1, N)
COLOR = ["BLUE", "PINK", "GREEN", "RED"]
for I in COLOR :
for J in range (0, PICKER):
print (I, end = " ")
print ()
(i) (ii) (iii) (iv)
BLUE BLUE PINK BLUEBLUE
PINK BLUEPINK PINKGREEN PINKPINK
GREEN BLUEPINKGREEN GREENRED GREENGREEN
RED BLUEPINKGREENRED GREENGREEN
Ans. Option (i) and (iv) are possible
The maximum value of PICKER is 3 and minimum is 1
[Link] are the possible outcome(s) for “Jumble String”, when following code will
be executed?
import random
stringN = "SimplyCoding"
char_list = list(stringN)
[Link](char_list)
stringN = ''.join(char_list)
print ( stringN )
1. ( CmodlnpgiSyi )
2. miSiypgoCdln
3. ‘lCSgpimiondy’
4. kpCyodigntS
Ans. (ii) will be printed
32. Which is the correct outcome executed from the following code?
import random
n1= [Link](1, 10, 2)
n2= [Link](1, 10, 3)
print (n1,"and",n2)
(i) 2 and 7 (ii) 3 and 4
(iii) 8 and 10 (iv) 8 and 9
Ans. (ii) is correct