RANDOM MODULE
[Link](2,4)
Random Module:
It is a build-in module in python 5. choice() :
It will use generate the random numbers. This method is used for random selection from the
list,tuple,string and any other sequence.
It can choose the random element from the list.
Syntax:
It can reshufle the string.
[Link](<sequence>)
It can also generate the floating point numbers.
Example:L=[11,22,33,44]
Functions of Randon Module:
[Link](L)
1. random():
6. shuffle() :
• This will generate floating values between 0
and 1 (including 0 but excluding 1).It does This method can shufle the item of a given list.
not require any argument.
Shufle list x in place and return None.
Synatx: [Link]()
Syntax:
2. randrange():
[Link](<sequence>)
• This method always return an integer
Example:
between given lower and upper limit
(excluding upper limit). L=[14,23,45,67]
Syntax: [Link](L)
[Link](20)
#It will generate random integers between 0,19
[Link](0,6,2)
# It will generate random value 0,2,4
3. randint() :
This method takes two arguments a,b in which first is
lower limit and second one is upper limit.
This function will return any integer number between
the two number including both limits.
[Link](1,6)
# It will generate random value 1,2,3,4,5,6
4. uniform() :
This method return any floating point number
between two given numbers.
Get a random number in the range [a,b] or [a,b]
depending or rounding
1
RANDOM MODULE
b can be anything between 1 to 6
Questions:
let b =6
Identify the correct output of the following [Link] for I in range(0,6,2)
write the Minimum and Maximum possible values of it will print W#s#o#
the variable b.(SQP 2024)
Q2.
import random import random
a=“Wisdom” X=[Link]()
Y=[Link](0,4)
b=[Link](1,6) print(int(X), “:” ,Y+int(X)) (SQP-2017)
for i in range(0,b,2):
a) 0:0 b)1:6 c) 2:4 d) 2:3
print(a[i],end=“#”)
Ans: Option a)
a)W# b) W#i# c) W#s# d)W#i#s# Solution:
Ans: option a,c
X=[Link]()
Solution: Here random() function will generate the values
between 0,1 excluding 1 so values may be
0 1 2 3 4 5 0,0.1,0.12 ........ so on.
W i s d o m
Y=[Link](0,4)
Value of Y will be 0,1,2,3,4
b can be anything between 1 to 6 print(int(X), “:” ,Y+int(X))
(Minimum=1,Maximum=6) here int(X) is always generate integer value that will be
let b =1 0 here
for I in range(0,1,2) so possible outputs will be
it will print W# 0:0 ,0:1 , 0:2 ,0:3 ,0:4
b can be anything between 1 to 6
let b =2 Q3.
for I in range(0,2,2) Import random
it will print nothing Values=[10,20,30,40,50,60,70,80]
BEGIN=[Link](1,3)
b can be anything between 1 to 6 LAST=[Link](2,4)
let b =3 for i in range(BEGIN,LAST+1):
for I in range(0,3,2) print(Value[i],end=”-”)
it will print W#s#
a) 30-40-50- c) 10-20-30-40-
b can be anything between 1 to 6 b) 30-40-50-60- d) 30-40-50-60-70-
let b =4
for I in range(0,4,2) Ans: option a)
it will print W#s# Min and Max value of BEGIN= 1,3
Min and Max value of LAST= 2,4
b can be anything between 1 to 6
let b =5 Solution:
for I in range(0,5,2) Value of BEGIN=1,2,3 Value of LAST=2,3,4
it will print W#s#o# Let assume BEGIN=1 and LAST=4
2
RANDOM MODULE
for i in range(1, 4+1) if pick=3 loop will execute 2 times
inside the loop print(I,end=””) will print
so possible output will start from index 1 and end at DELHIDELHI
index 4 MUMBAIMUMBAI
so output will be in between 20-30-40-50- CHANNAICHANNAI
KOLKATAKOLKATA
Q4: Questions for Practice:
import random What will be the expected output after execute the
pick=[Link](0,3) following codes and write the maximum and minimum
city=["DELHI","MUMBAI","CHENNAI","KOLKATA"] value for the assigned variable of randint() function
for I in city:
for j in range(1,pick):
print(I,end="") (NOTE:More then one option can be correct in random
print() module based Question )
a)DELHI b)DELHI 1.
MUMBAI DELHIMUMBAI import random
CHENNAI DELHIMUMBAICHANNAI a="programming"
KOLKATA b=[Link](2,8)
for i in range(b):
c) d) DELHI if i%2==0:
DELHIDELHI MUMBAIMUMBAI print(a[i],end="*")
MUMBAIMUMBAI CHANNAICHANNAI
CHANNAICHANNAI KOLKATA a) p*o*r*m* b) p*o c)prg* d)pog*
KOLKATAKOLKATA
2.
Ans : Option a),c) import random
Min pick is 0 A=[10,30,40,50,60,70]
Max pick is 3 From=[Link](1,3)
To=[Link](2,4)
0 1 2 3 for k in range(From,To+1):
DELHI MUMBAI CHANNAI KOLKATA print(A[k],end="#")
Solution
a) 10#40#70# b) 30#40#50#
for I in city: c)50#60#70 d)40#50#70
this loop have the value of I
DELHI,MUMBAI,CHANNAI,KOLKATA 3.
import random
for j in range(1,pick): A=["EAST","WEST","NORTH","SOUTH"]
if pick=0 loop not execute N=[Link](1,3)
if pick=1 loop not execute out=""
if pick=2 loop will execute one time for i in range(N,1,-1):
inside the loop print(I,end=””) will print out=out+A[i]
DELHI print(out) (SQP 2018)
MUMBAI
CHANNAI a)SOUTHNORTH b)SOUTHNORTHWEST
KOLKATA c)SOUTH d)EASTWESTNORTH
3
RANDOM MODULE
8.
import random
4.
P=[Link](1,3)
import random Colour=[“yellow”,”purple”,”orange”,”black”]
a="Exploration" for I in Colour:
b=[Link](1,6) for j in range(1,P):
for i in range(b,len(a)): print(I,end=””)
if i%2==0: print()
print(a[i],end="@")
a)E@p@o@a@i@n@ b)l@r@t@o@n@ a) yellow c) yellowyellow
c)x@l@r@t@o@n@ d)p@o@a@i@n@ purple purplepurple
orange orangeorange
black blackblack
5.
import random b) yellowyellowyellow d)yellow
NAV=["LEFT","FRONT","RIGHT","BACK"] purplepurple orangeorange
NUM=[Link](1,3) orangeorangeorange purplepurple
NAVG="" blackblack black
for C in range(NUM,1,-1):
NAVG=NAVG+NAV[C]
print(NAVG)
a)BACKRIGHT b) BACKRIGHTFRONT
c) BACK d) LEFTFRONTRIGHT
6.
import random
points=[20,40,10,30,15]
points=[30,50,20,40,45]
Begin=[Link](1,3)
last=[Link](2,4)
for i in range(Begin,last+1):
print(points[c],"#")
a)BACKRIGHT b) BACKRIGHTFRONT
c) BACK d) LEFTFRONTRIGHT
7.
import random
points=[20,40,10,30,15]
points=[30,50,20,40,45]
Begin=[Link](1,3)
last=[Link](2,4)
for i in range(Begin,last+1):
print(points[i],end="#")
a) 20#50#30 b) 20#40#45 c) 50#20#40 d) 30#50#20