WS Random Outputs
WS Random Outputs
Random Module
The random provides random-number generators. A random number means – a number
generated by chance, i.e., randomly.
To use random number generators in a Python program, you first need to import module
random.
e.g.,
import random
Some most common random number generator functions in random module are:
Sno Function Description
1. random( ) It returns a random floating point number
N in the range (0.0, 1.0), i.e., 0.0<= N
<1.0. Notice that the number generated
with random( ) will always be less than
1.0 (only lower range-limit is inclusive).
Also remember that it generates a floating
point number.
Sample codes along with their output using functions of random module.
To generate a random floating-point number between 0.0 to 1.0, simply use random( ):
>>> import random
>>> print(random.random( )) # The output generated is between range (0.0, 1.0)
0.022353193431
To generate a random integer in range 15 to 35 using randint( ), write :
>>> print(random.randint(15, 35))
16
#The output generated is integer between range 15 to 35
To generate a random number in the range 23 . . .47 with a step 3 or 0 . . .235, after importing
random module.
>>>random.randrange(23, 47, 3)
38
>>>random.randrange(235)
126
1 What are the possible outcome(s) executed from the following code ? Also specify the
maximum and minimum values that can be assigned to variable NUM.
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)
2 What possible outputs 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 c.
import random
temp=[10,20,30,40,50,60]
c=random.randint(0,4)
for i in range(0, c):
print(temp[i],”#”)
3 What possible output(s) are expected to be displayed on screen at the time of execution of
the program from the following code? Also specify the minimum values that can be assigned
to each of the variables BEGIN and LAST.
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],"-",end=” “)
4 What possible output(s) is/ are expected to be displayed on screen at the time of execution of
the program from the following code? Also specify the maximum values that can be
assigned to each of the variables start and end.
import random
POINTS = [ 30,50,20,40,45]
start = random.randint(1,3)
end = random.randint(2,4)
for c in range (start, end+1):
print(POINTS[c] , ‘#’)
5 What possible output(s) are expected to be displayed on screen at the time of execution of
the program from the following code? Also specify the maximum values that can be
assigned to each of the variables Lower and Upper
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 = ‘#’)
6 What 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
PICK = random.randint(0,3)
CITY=[“DELHI”, “MUMBAI”, ”CHENNAI”, “KOLKATA”]
for I in CITY:
for J in range(1, PICK):
print(I, end =’ ‘)
print( )
a) b) c) d)
RED* YELLOW* WHITE*WHITE* YELLOW*
WHITE* WHITE* YELLOW*YELLOW* WHITE*WHITE*
BLACK* BLACK* BLACK*BLACK* BLACK*BLACK*BLACK*
RED* RED* RED*RED* RED*RED*RED*RED*RED*
Ans)b
9 What possible output(s) are expected to be displayed on screen at the time of execution of
the following program:
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 ii)5#25#20
20#25#25 25#20#15
iii)30#20#20 iv)10#15#25#
20#25#25 15#20#10#
10 What possible output from the given options is expected to be displayed when the following
Python code is executed?
import random
Signal = [‘RED’, ’YELLOW’, ’GREEN’]
for K in range (2, 0, -1):
R = random.randrange(K)
print(Signal[R], end = ‘#’)