Random Module in Python 2-3 Marks
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 |
random( ) | returns a random floating point number N in the range(0.0, 1.0). The generated number is greater than or equal to 0.0 and less than 1.0. | >>> import random >>> random.random() 0.896986986196 |
randint(a,b) | returns a random integer N in the range(a,b) i.e. a <=N <= b. | >>>random.randint(1,4) 4 >>>random.randint(1,4) 2 >>>random.randint(1,4) 1 |
uniform(a, b) | returns a random floating point number N in the range(a,b) i.e. a <=N <= b. | >>>random.uniform(11,55) 32.42694236141252 |
randrange(stop) randrange(start, stop, step) | it returns a randomly selected element from range(start, stop, step) | >>>random.randrange(5) 4 >>>random.randrange(5,15,4) 9 |
Application/Output-Based Question Answer
5. Write the statement to generate a random floating point number between 25 to 50.
6. Write the statement to generate a random integer number between 25 to 50.
7. What could be the minimum possible and maximum possible numbers by the following code?
import random
print(random.randint(15, 30) - 7)
8. Consider the following code:
import random
print( int(20 + random.random() * 5), end = ' ')
print( int(20 + random.random() * 5), end = ' ')
print( int(20 + random.random() * 5), end = ' ')
print( int(20 + random.random() * 5))
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
9. Consider the following code:
import random
print( 100 + random.randint(5,10), end = ' ')
print( 100 + random.randint(5,10), end = ' ')
print( 100 + random.randint(5,10), end = ' ')
print( 100 + random.randint(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
10. Consider the following code:
import random
r = random.randrange (100, 999, 5) #line 2
print(r, end = ' ')
r = random.randrange(100, 999, 5)
print(r, end = ' ')
r = random.randrange (100, 999, 5)
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
11. Consider the following code:
import random
r = random.randrange (50, 100) - 50 #line 2
print(r, end = ' ')
r = random.randrange (50, 100) - 50
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
12. Consider the following code:
import random
AR = [20, 30, 40, 50, 60, 70]
Lower = random.randint(1, 3)
Upper= random.randint(2, 4)
for K in range (Lower, Upper+1):
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#
13. Consider the following code:
import random
STRING="CBSEONLINE"
NUMBER = random.randint (0, 3)
N=9
while STRING[N] != "L":
print (STRING[N] + STRING[NUMBER] + "#", end = " ")
NUMBER = NUMBER +1
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#
14. What will be the output of the following Python code?
import random
List=["Delhi","Mumbai","Chennai","Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")
a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # 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]
import random
Signal = [ 'Stop', 'Wait', 'Go' ]
for K in range(2, 0, -1):
R = randrange(K)
print (Signal[R], end = ' # ')
(a) Stop # Wait # Go #
(b) Wait # Stop #
(c) Go # Wait #
(d) Go # Stop #
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]
import random
Colours = ["VIOLET","INDIGO","BLUE","GREEN", "YELLOW","ORANGE","RED"]
End = randrange(2)+3
Begin = randrange(End)+1
for i in range(Begin,End):
print(Colours[i],end="&")
(i) INDIGO&BLUE&GREEN&
(ii) VIOLET&INDIGO&BLUE&
(iii) BLUE&GREEN&YELLOW&
(iv) GREEN&YELLOW&ORANGE&
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]
import random
VALUES=[10,20,30,40,50,60,70,80]
BEGIN = random.randint(1, 3)
LAST = random.randint(BEGIN, 4)
for I in range(BEGIN, LAST+1):
print(VALUES[I], "-",)
(i) 30 – 40 – 50 –
(ii) 10 – 20 – 30 – 40 –
(iii) 30 – 40 – 50 – 60 –
(iv) 30 – 40 – 50 – 60 – 70 –
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]
import random
VAL=[80, 70, 60, 50, 40, 30, 20, 10]
Start = random.randint(1, 3)
End = random.randint(Start, 4)
for I in range(Start, End+1):
print(VAL[I], "*", )
(i) 40 * 30 * 20 * 10 *
(ii) 70 * 60 * 50 * 40 * 30 *
(iii) 50 * 40 * 30 *
(iv) 60 * 50 * 40 *
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]
import random
POINTS = [30,50,20,40,45];
BEGIN = random.randint(1, 3)
LAST = random.randint(2, 4)
for C in range(BEGIN, LAST+1):
print(POINTS[C], "#",)
(i) 20#50#30#
(ii) 20#40#45#
(iii) 50#20#40#
(iv) 30#50#20#
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. [CBSE Board 2017 ]
import random
NAV = ["LEFT", "FRONT", "RIGHT", "BACK"]
NUM = random.randint(1,3)
NAVG = ""
for C in range (NUM, 1, -1):
NAVG = NAVG + NAV[C]
print(NAVG)
(i) BACKRIGHT
(ii) BACKRIGHTFRONT
(iii) BACK
(iv) LEFTFRONTRIGHT
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. [CBSE Board 2017 ]
import random
NAV = ["LEFT", "FRONT", "RIGHT", "BACK"]
NUM = random.randint(1, 3)
NAVG = ""
for C in range (NUM, 1, -1):
NAVG = NAVG + NAV[C]
print(NAVG)
(i) BACKRIGHT
(ii) BACKRIGHTFRONT
(iii) BACK
(iv) LEFTFRONTRIGHT
22. What possible output(s) are expected to be displayed on the screen at the time of execution of the following program : [CBSE 2023]
import random
M- (5, 10, 15, 20, 25, 30]
for i in range (1, 3) :
first = random. randint (2, 5) - 1
sec = random. randint (3, 6) - 2
third=random. randint (1, 4 )
print (M[first] , M[ sec], M[third] , sep = "#")
(i) 10#25#15
20#25#25
(ii) 5# 25#20
25#20#15
(iii) 30#20#20
20#25#25
(iv) 10#15#25#
15#20#10#