Python modules – Random module
___________________________________________________________________________________________________
A module is a piece of Python code. A module allows you to logically organize
your Python code. A module is a file that contains Python statements and
definitions. The modules in Python have the .py extension.
Python itself is having a vast module library and packages with the default
installation. To use any package in your code, you must first make it accessible.
You have to import it using the import directive. For example,
import math
import random
import string
Notice that after using the above three import statements, you can access all
built-in functions into your current Python program or at interactive mode.
Importing Python Module
You can use any Python source file as a module by executing an import
statement in some other Python source file or in interactive mode. When a
module is imported, Python runs all of the code in the module file.
The importance of import statement is:
The import statement makes a module and its contents available for use.
The import statement evaluates the code in a module, but only the first
time when any given module is imported in an application.
A module is loaded only once, regardless of the number of times it is
imported.
Python imports are case-sensitive
The general format of import statement is:
import module_name1 [, module_name2 [,... module_nameN ]
Using random module
Random numbers are heavily used in Computer Science for programs that involve
games or simulations.
Python has a random module to generate random numbers.
>>> import random
>>> print (random)
<module 'random' from 'C:\\Python37\\lib\\random.py'>
1. random() – This function generates a random floating number from 0 to
1
including 0 , but excluding 1.
>>> import random
>>> print (random.random())
Output:
0.23604321584998056
2. randrange()- This method generates an integer number between its lower
and upper argument.
>>> import random
>>> print(random.randrange(10)) #8
>>> print(random.randrange(10)) #7
Function generate a number between 0 to 9 not
including 10. Just like range ( ) function .
>>> print(random.randrange(1,10)) #3
>>> print(random.randrange(1,10)) #7
>>> print(random.randrange(1,10,3)) #4
3. randint( )- This function generate a random integer number. This function
accepts two arguments : a lower and a highest number . It will
include both number (lower and upper).
>>> print(random.randint(1,5)) #5
Remember that this function return only one number either
1 , 2 , 3 , 4 , or 5
4. choice ( )- This function is used for making a random selection from a
sequence like a list, tuple or string .
import random
l=[1,2,3,4,5]
print(random.choice(l))
output: 3
5. shuffle( ) - This method is used to shuffle the contents of a list ( that
is ,
generate a random permutation of a list in –place)
import random
l=[1,2,3,4,5]
print(random.shuffle(l))
print(l)
output : [1, 2, 3, 5, 4]
Assignment of Random module
____________________________________________________
1. 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) 30 - 40 - 50 - 60 - 70 -
2. 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)
print(NUM)
NAVG = ""
for c in range (NUM,1,-1):
NAVG=NAVG + NAV[c]
print (NAVG)
(i) BACKRIGHT (ii) BACKRIGHTFRONT
(iii) BACK (iv) LEFTFRONTRIGHT
3. Study the following program and select the possible output(s) from the options (i)
to
(iv) following it. Also, write the maximum and the minimum values that can be
assigned to the variable Y.
import random
X=random.random()
Y=random.randint(0,4)
print(int(X), ":" , Y + int(X))
(i) 0:0 (ii) 1:6 (iii) 2:4 (iv) 0:3
4. 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],end=”#”)
(i)10#20# (ii) 10#20#30#40#50# (iii) 10#20#30# (iv)
50#60#
5. What possible outputs(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 FROM and TO.
import random
AR=[20,30,40,50,60,70];
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”#“)
(i) 10#40#70# (ii) 30#40#50# (iii) 50#60#70# (iv) 40#50#70#
6. Observe the following program and answer the questions that follow: import
random
import random
X=3
N=random.randint( 1,X)
for i in range( N ):
print( i , ‘#’, i+1 )
a. What is the minimum and maximum number of times the loop will execute?
b. Find out, which line of output(s) out of (i) to (iv) will not be expected from
the program?
i). 0#1 ii). 1#2 iii). 2#3 iv). 3#4
7. What possible output(s) are expected to be displayed on screen at the time of
execution of the program from the following Python code ? Also specify the
minimum
values that can be assigned to each of the variables START and END.
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],end="*", )
(i) 40 * 30 * 20 * 10 * (ii) 70 * 60 * 50 * 40 * 30 *
(iii) 50 * 40 * (iv) 60 * 50 * 40 * 30 *
8. 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 BEGIN and LAST.
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],end="#")
(i) 20#50#30# (ii) 20#40#45# (iii) 50#20#40# (iv) 30#50#20#
9. What are the possible outcome(s) executed from the following code ? Also specify
the
maximum and minimum values that can be assigned to variable N.
import random
SIDES=["EAST","WEST","NORTH","SOUTH"]
N=random.randint(1,3)
OUT=""
for I in range(N,1,–1):
OUT=OUT+SIDES[ I ]
print ( OUT )
(i) SOUTHNORTH (ii) SOUTHNORTHWEST (iii) SOUTH (iv) EASTWESTNORTH
10. In the following program, if the value of N given by the user is 20, what
maximum
and minimum values the program could possibly display?
import random
N=int(input(“enter the value of N”))
Guessnum=random.randrange(0,N-10)+10
print(Guessnum )
11. In the following program, if the value of Guess entered by the user is 65, what will
be the expected output(s) from the following options (i), (ii), (iii) and (iv)?
import random
Guess=int( input(“ enter the value of Guess”))
for I in range(1,5):
New=Guess + random.randrange(0,I )
print(chr(New),end=' ')
i). A B B C ii). A C B A iii). B C DA iv). C A B D
12. Observe the following program RANDNUM.PY carefully. If the value of VAL entered
by the user is 5, choose the correct possible output(s) from the options from ( i) to(
iv)
and justify your option.
import random
n=1
VAL=int(input(“enter the value of VAL”))
Rnd=8 + random.randrange(0,VAL) * 1
while(n<=Rnd):
print(n,end=" ")
n+=1
i) 1 2 3 4 5 6 7 8 9 10 11 12 13 ii) 0 1 2 3
iii) 1 2 3 4 5 iv) 1 2 3 4 5 6 7 8
13. Observe the following program carefully & choose the correct possible output
from the options (i) to (iv) justifying your answer.
import random
RN=random.randint(0,3)+5;
for i in range(1,RN):
print( i, end=' ')
# Output Options:
i). 0 1 2 ii). 1 2 3 4 5 6 7 iii). 4 5 6 7 8 9 iv). 5 6 7 8 9 10 11 12
14. In the following PYTHON program what is the expected value of Myscore from
Options (i) to (iv) given below. Justify your answer.
import random
Score=[ 25,20,34,56, 72, 63]
Myscore = Score[ 2 + random.randrange(0,2)]
print(Myscore)
i). 25 ii). 34 iii). 20 iv). None of the above
15. The following code is from a game, which generates a set of 4 random numbers.
Praful is playing this game , help him to identify the correct option(s) out of four
choice given below as the possible set of such numbers generated from the
program code so that he wins the game . Justify your answer.
import random
low=25
point =5
for i in range (1,5):
Number=low + random.randrange(0,point);
print(Number , end=" :")
point-=1;
i) . 29: 26:25 :28 : ii). 24: 28:25 :26 : iii). 29: 26:24 :28 : iv). 29: 26:25 :26 :
16. Study the following program and select the possible output’s from it:. Also justify
your answer.
import random
Max=3
Div=1+random.randrange(0,Max)
for N in range(1,5):
print(100%Div,end="#")
i). 0#0#0#0# ii). 1#1#1#1# iii). 2#2#2#2# iv). 3#3#3#3#
17. Study the following program and select the correct option(s) out of the four
choices given below as the possible set of such numbers generated from the
program code. Justify your answer.
import random
MIN = 25
SCORE = 10
for i in range (1,5):
Num = MIN + random.randrange(0,SCORE)
print(Num,end=":")
SCORE-=1;
i) 34:31:30:33: ii) 29:33:30:31: iii) 34:31:30:31: iv) 34:31:29:33:
18. Study the following program and select the possible output from it:
import random
Max=3
Number=50+random.randrange( 0,Max)
for i in range ( Number, 50-1,-1):
Print(i,end=’#’)
(i) 53#52#51#50# (ii) 50#51#52# (iii) 50#51# (iv)
51#50#
19. Read the program carefully and select the possible output(s) from it:
import random
MAX = 3
digit =80 + random.randrange (0,MAX )
for i in range (digit, 80-1,-1):
print(i, end=’$’)
(i) 83$82$81$80$ (ii) 80$81$82$ (iii) 80$81$ (iv) 81$80$
20. Find the output of the following program:
import random
serial= ['A', 'B', 'C', 'D' ]
number = [ 2, 6, 7, 3 ]
print(“ The winner is : ", end=‘ ’ )
print(serial [random.randrange(0,3)]
for i in range (0,4):
print( number[2 + random.randrange(0,2) - 1 ], end =’ ‘ )
(i) The winner is : A2776 (ii) The winner is : D6766
(iii) The winner is : B6767 (iv) The winner is : C3672
21. Given the following code, choose the correct option(s) from (a) to (d) for output of
the code. Write the Minimum and Maximum values that can be assigned to
variable L.
import random
max = 3;
a= [10,20,30 ]
L = random.randrange(0,len(a))
if(L%2):
a[L]=a[L]+1;
else:
a[L]=a[L]-1;
for i in a :
print(i , end=’ # ‘)
(i) 9 #21 # 29 # (ii) 9 # 20 # 29 #
(iii ) 10 # 21 # 30 # (iv) 11 # 20 # 31 #
22. Study the following program and select the possible output(s) from the options (i)
to (iv) following it. Also, write the maximum and the minimum values that can be
assigned to the variable VAL.
import random
VAL=random.randrange(3)+2
GUESS=”ABCDEFGHIJK”
for I in range(1,val+1):
for J in range (VAL, 8)
print(GUESS[J],end=’ ‘)
print()
(i) (ii) (iii) (iv)
BCDEFGH CDEFGH EFGH FGHI
BCDEFGH CDEFGH EFGH FGHI
EFGH FGHI
EFGH FGHI
23. Find out the possible output(s) .
import random
Arr=[ 9,6]
Chance=random.randrange(0,2)+10
for I in range (2):
N=random.randrange(0,2)
print(Arr[N]+Chance , end="*")
i). 9*6* ii). 19*17* iii). 19*16* iv). 20*16*
24. Find out the possible output(s) .
import random
X =[100,75,10,125]
Go =random.randrange(0,2)+2
for i in range (Go,4):
print(X[i],end="$$")
i). 100$$75 ii). 75$$10$$125$$ iii). 75$$10$$ iv).10$$125$
Answers
1 (i) Begin=1, last=1 2 (i) min=1, max=3
3 (i) & (iv) min=0 , max= 4 4 (i) & (iii) min=0 , max= 4
5 (ii) from=3, To=4 6 (i), (ii) & (iii)
7 (iii) Start=1, End=1 8 (ii) & (iii) Begin=3, last=4
9 (i) min=1, max=3 10 10 & 19
11 (i) 12 (iv)
13 (ii) 14 (ii)
15 (iv) 16 (i) &(ii)
17 (ii) & (iii) 18 (iv)
19 (iv) 20 (iii)
21 (iii) 22 (ii) & (iv) min=2, max=4
23 (iii) 24 (iv)