0% found this document useful (0 votes)
58 views4 pages

WS Random Outputs

The document discusses the random module in Python which provides functions for generating random numbers. It describes functions like random(), randint(), uniform(), and randrange() giving their purpose and examples of using each.

Uploaded by

iammaar25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

WS Random Outputs

The document discusses the random module in Python which provides functions for generating random numbers. It describes functions like random(), randint(), uniform(), and randrange() giving their purpose and examples of using each.

Uploaded by

iammaar25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WORK SHEET - RANDOM OUTPUT (BOARD QUESTIONS)

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.

2. randint(a, b) It returns a random integer N in the range


(a, b), i.e., a <= N <= b (both range-
limits are inclusive). Remember, it
generates an integer.
3. random.uniform(a,b) It returns random floating point number
such that
a <= N <= b for a<=b
b <= N <= a for b<a
4. random.randrange(stop) It returns a randomly selected element
random.randrange(start, stop[, step]) from range(start, stop, step).

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)

(i) BACKRIGHT (ii) BACKRIGHTFRONT (iii) BACK (iv) LEFTFRONTRIGHT

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],”#”)

(i) 10#20# (ii) 10#20#30#40#50# iii) 10#20#30# (iv) 50#60#

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=” “)

i) 30 - 40 - 50 - ii) 10 - 20 - 30 - 40 - iii) 30 - 40 - 50 - 60 - iv) 20 – 30 – 40 – 50 –

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] , ‘#’)

i) ii) iii) iv)


50# 40# 50# 20#
20# 30# 40# 40#
20# 45#

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 = ‘#’)

i)10#40#70# ii) 30#40#50# iii) 50#60#70# iv)40#50#70#

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( )

i) ii) iii) iv)


DELHIDELHI DELHI DELHI DELHI
MUMBAIMUMBAI DELHIMUMBAI MUMBAI MUMBAIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI CHENNAI KOLKATAKOLKATAKOLKATA
KOLKATAKOLKATA KOLKATA
7 Identify the correct output:
import random
for N in range(2,5,2):
print(random.randrange(1,N), end = ‘#’)

a)1#3#5# b)2#3# c)1#4# d)1#3#

8 What possible output(s)will be obtained when the following code is executed?


import random
myNumber=random.randint(0,3)
COLOR=[“YELLOW”,”WHITE”,”BLACK”,”RED”]
for I in COLOR:
for J in range (1, myNumber):
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 = ‘#’)

a)YELLOW # RED # b) RED # GREEN #


c)GREEN # RED # d) YELLOW # GREEN #

You might also like