Random
Function using random module
The random() function is used to return a number randomly between the range of two numbers. It is available in the random module of the Python language. It means, you need to import the random module for using this function. This function is used in various forms as shown below:
(i) random() : |
It will generate a random floating point number n in the range(0.0, 1.0) (i.e., 0.0s n<1.0). This function doesn't need any argument (i.e., empty braces). Example: >>> import random >>> random.random() 0.0881805094026008 >>> random.random() 0.275467541270612 >>> random.random() 0.9822090761477658 >>> random.random() 0.1360804918081625 |
(ii) randint(<start>,<stop>) : |
This
function will return a random integer number between the specified range of
integer numbers. Example random.random(1, 100): It will generate a random
integer number between 1 and 100 (both inclusive). >>> random.randint(1,100) 65 >>> random.randint(1,100) 77 >>> random.randint(1,100) 6 >>> random.randint(1,100) 72 >>> random.randint(1,100) 9 |
(iii) randrange(<start>,<stop>,<step>):
|
It returns a randomly selected number 'n' from the range specified by the start, stop and step arguments. By default, the start value is 0 and the step value is 1. Example: >>> random.randrange(5,100,10) 85 >>> random.randrange(5,100,10) 55 >>> random.randrange(5,100,10) 35 Example: >>> random.randrange(1,10) 2 >>> random.randrange(1,10) 1 >>> random.randrange(1,10) 5 >>> random.randrange(1,10) 7 |
random.uniform() |
This function uses two integer arguments and returns a floating type random value between them (both exclusive). Syntax: random.uniform(int a, int b) Where, the first argument specifies the lowest possible outcome and the second argument specifies the highest possible outcome. For
example: (i) random.uniform (5, 10) When the above statements are executed, they will result in the random floating values between 5 and 10. >>> random.uniform (5, 10) 5.322018058716384 >>> random.uniform (5, 10) 5.671189348754382 |
random.choice() |
This is used to select any function element from any sequence like any string, list, tuple or dictionary. Example: import random lst=[10,20,30,40,50] print(random.choice(lst)) Output: 40 (It could be nay other element from the list) Example import random lst=['Python','C++','C','JAVA','ASP','JSP'] print(random.choice(lst)) Output: JAVA (It could be nay other element from the list) Example import random x = "WELCOME" print(random.choice(x)) Output: Resturn random character string. |
Random.shuffle() |
This function is used to shuffle (change the sequence) the elements in the list. Example: import random lst=['Python','C++','C','JAVA','ASP','JSP'] random.shuffle(lst) print(lst) Output: ['JAVA', 'C++', 'C', 'ASP', 'Python', 'JSP'] |
Python Program to generate one-time password (OTP)
import
math, random
digits
= "0123456789"
OTP
= ""
for
i in range(4) :
OTP += digits[math.floor(random.random() * 10)]
print("OTP of 4 digits:", OTP)
Number Guessing Game using Python
import
random
n
= random.randrange(1,10)
guess
= int(input("Enter any number: "))
while
n!= guess:
if guess < n:
print("Too low")
guess = int(input("Enter number
again: "))
elif guess > n:
print("Too high!")
guess = int(input("Enter number
again: "))
else:
break
print("you
guessed it right!!")
Q1. Consider the code given below:
import random
r = random.randrange (100, 999, 5)
print(r, end = ' ')
r = random.randrange(100, 999, 5)
print(r, end = ' ')
r = random.randrange (100, 999, 5)
print(r)
Which of the following are the possible outcomes of the above code?
Also, 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
Ans
: (a) & (d)
Q2. Consider the code given below:
import random
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r, end = ' ')
r = random.randint (10, 100) - 10
print(r)
Which of the following are the possible outcomes of the above code?
Also, what can be the maximum and minimum number
generated by line 2?
(a) 12 45 22 (b) 100
80 84 (c) 101 12 43 (d) 100 12 10
Ans
: (a)
Q3. Consider the code given below:
import random
r= random.random() * 10
print(r, end = ' ')
r = random.random() * 10
print(r, end =' ')
r = random.random() * 10
print(r)
Which of the following are the possible outcomes of the above code?
Also, what can be the maximum and minimum number generated by line 2?
(a) 0.5 1.6 9.8 (b)
10.0 1.0 0.0 (c)
0.0 5.6 8.7 (d)
0.0 7.9 10.0
Ans
: (a) & (c)
Q4.
Consider the code given below:
import statistics as st
v = [7, 8, 8, 11, 7, 7]
m1 = st.mean(v)
m2 = st.mode(v)
m3 = st.median(v)
print(m1, m2, m3)
Which of the following is the correct output of the above code?
(a) 7 8 7.5 (b)
8 7 7 (c) 8 7 7.5 (d) 8.5 7 7.5
Ans
: (c)
Q5.
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 variable 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=”#”)
Output
Options:
(a)
10#40#70# (b)
30#40#50# (c)
50#60#70# (d)
40#50#70#
Ans
: (b), Minimum value of Lower: 3, Maximum value of Upper: 4
Q6.
What possible output(s) are excepted to be displayed on the screen after
executing the following code?
import random
Lst=[0,
1, 2, 3, 4, 5, 6, 7, 8, 9]
first=
random.randrange(2,5)
second
= random.randrange(1,3)
for
L in range (second, first):
print(Lst[L], end = “@”)
Output
Options:
(a)
1@2@3@ (2) 0@1@2@3@ (c) 2@3@ (d) 2@3@4@
Ans:
(a) & (c)
Q7.
What is possible output(s) is/are expected to be displayed on screen after
execution of the following program?
import
random
Guess=65
for
a in range(0,1):
for b in range (1,5):
New =
Guess+random.randrange(0,b)
print(chr(New), end= ‘ ’)
(a)
A B
B C (b) A
C B A (c)
B C
D A (d) C A B D
Ans:
(a)
Q8.
What is possible output(s) is/are expected to be displayed on screen after
execution of the following program?
import
random
MIN=
25
SCORE=5
for i in range(1,5):
Num=MIN+random.randrange(0,SCORE)
print(Num, end = “&”)
SCORE -=1
Output
Options:
(a)
29&26&28&27& (b)
25&26&27&28& (c) 29&25&27&25& (d) 25&26&25&26&
Ans:
(c) & (d)
Q9.
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 the variables X and Y.
import
random
X
= random.randrange(0,3)+2
Y
= random.randrange(1,3)+2
for
i in range(X):
print("#", end= “”)
print("-",
end=" ")
for
i in range(Y):
print(“@”, end="")
Output
Options:
(a)
##-@@@ (b) ###-@ (c) ######-@@@ (d) ###-@@@@
Ans:
(d),
Maximum value for is X: 4, , Manimum value for is Y: 4,
Q10.
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 a variable val, in each iteration.
import
random
msg
= "VIRTUAL"
index
= len(msg)-1
while
msg[index] != 'R':
val = random.randrange(2,index)
print(msg[val]+"#",end="")
index = index-1
(a)
A#R#U#R# (b) A#R#T#T# (c) U#A#R#R# (d) U#R#T#R#
Ans
: (d) , Max value=5
Q11.
What possible output(s) are expected to be display execution of the following
code?
import
random
HIGH
= 20
NUM
= 15
for
i in range(1,5):
N = HIGH+random.randint(11, NUM)
print(N, end = “:”)
NUM - = 1
Outputs:
(a)
35:31:34:32: (b) 35:34:33:32: (c)33:34:33:32: (d) 32:31:35:31:
Ans:
(b)
Q12.
What possible output(s) are expected to be displayed on one of the execution of
the following code is specify the maximum and minimum values which can be
assigned to the ‘Target’ by randrange() function of random module.
import
random
X=
[11000, 999,99, 9]
Target
= random.randrange(0,4)
for
i in range(Target):
print(X[i], “#”)
(a)
1000# 999# 99# |
(b)
999# 99# 9# |
(c)
1000# 1000# 999# |
(d)
999# 99# |
Ans:
(a) , Maximum:3 , Minimum : 0
Q13.
From the following Python code shown, below, find out the possible output(s)
from the suggested options.
import
random
def
week_days():
days=[“Mon”, “Tue”, “Wed”, “Thu”, “Fri”]
v = random.randrange(len(days)-1)
for i in range(v,0,-1):
print(days[i], “-“, i+1)
week_days()
(a)
Thu- 4 Wed-3 Tue-2 |
(b)
Wed-3 Tue-2 |
(c)
Wed -3 Tue-2 Mon-1 |
(d)
Fri-5 Thu-4 |
Ans
: (a) & (b)
Q14. 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 both the
variables Beg and End.
import
random
score=
[11, 22, 33, 44, 55]
Beg
= random.randrange(3)
End
= random.randint(2,3)
for
C in range(Beg, End):
print(Score[C],'#')
(a)
11# 22# 33# |
(b)
22# 33# 44# |
(c)
22# 33# |
(d)
33# 44# |
Ans: (a), (c) Max value to a variable Beg: 2, Min
value to a variable End: 3
Q14. 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 both the
variables Y.
import
random
X
= random.randrange(1,4)
Y=
random.random()
print(“%2d”,
%(Y+X), “@”, X)
(a)
00 @ 0 (b) 01 @ 1 (c) 02 @ 2 (d)
04 @ 4
Ans:
(b) & (c), Max value to Y:0, Min
value to Y: 1
Q15.
What possible output(s) are expected to
be displayed on screen at the time of execution of the program from the
following code?
import
random
n=4
while
n>1:
val= random.randint(2,n)
for m in range(val)
if m%2= = 0:
print(m*2, end= “:”)
n=1
Output
Option
(a)
0:4:0:0: (b)
0:0:0: (c) 0:4:0:4:0 (d) 0:4:0:4
Ans:
Q16. 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 first and last values that can be assigned to a the
variables val in the last iteration.
import
random
msg=
“Luck”
x=1
for
a in msg:
val=random.randint(0, len(msg)-x)
print(mag[val]+ “:”, end= “”)
x=x+1
(a)
c : k : u : k : (b) k : u : c : c : (c)
l : u : u: c : (d) l : c : u : l
:
Q17.
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 the variables ‘Assigned’.
import
random
Ar=
[10,7]
Assigned
= random.randrange(10)+1
for
C in range(0,2,1):
R=random.randint(1,2)-1
print(Ar[R]+Assigned, “@”, end= “ ”)
(a)
11@8@ (b)
20@21@ (c) 21@20@ (d) 8@11@
Q18. 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 the variables ‘high’.
import
random
Ar=[11, 22, 33, 44, 55, 66 ]
low=random.randint(1,3)
high=random.randint(2,4)
for
c in range(low, high+1):
print(Ar[c], end= “:”)
(a)
11 : 22 : 33 : (b) 22 : 33 : (c)
11 : 22: (d) 44 : 55 : 66
:
Q19.
What possible output(s) is/are expected to be displayed on screen at the time
of execution of the program from the following code?
from
random import randint
Vibgyor=[[‘V’,
‘Violet’], [‘I’, ‘Indgo’], [‘B’, ‘Blue’], [‘G’, ‘Green’],[‘Y’, ‘Yellow’],[‘O’,
‘Orange’],[‘R’, ‘Red’]]
for
i in range(3):
first=randint(0,1)
last=randint(1,2)+1
print(Vibgyor[last-first], end= ‘:’)
(a)
[‘G’, ‘Green’]: [‘G’, ‘Green’]: [‘Y’,
‘Yellow’]:
(b)
[‘G’, ‘Green’]: [‘B’, ‘Blue’]: [‘G’,
‘Green’]:
(c)
[‘V’, ‘Violet’]: [‘B’, ‘Blue’]: [‘B’,
‘Blue’]:
(d)
[‘I’, ‘Indgo’]: [‘B’, ‘Blue’]: [‘B’,
‘Blue’]:
Q20. What are
the possible outcome(s) executed from the following code? Also specify the
maximum and minimum values that can be assigned to variable NUMBER.
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
(a)
ES#NE#IO# (b)LE#NO#ON# (c)NS#IE#LO# (d)EC#NB#IS#
Ans: Outcomes are
(i)
Maximum value of NUMBER is 6.
Minimum value of NUMBER is 3.
Because Minimum value of NUMBER from random.randint (0, 3)
is 0 and while loop will run only 3 times. So,
Minimum value of NUMBER is 3.
Maximum value of NUMBER from random.randint (0, 3) is 3 and while loop will run only 3 times. So,
Maximum value of NUMBER is 6.
Q21.
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.
(i) 20 22 24 25 (ii) 22 23 24 25 (iii) 23 24 23 24 (iv) 21 21 21 21
Q22. 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.
(i)
102 105 104 105 (ii) 110 103 104 105 (iii) 105 107 105 110 (iv) 110 105 105 110
Q23.
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) DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
(ii) DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
(iii) DELHI
MUMBAI
CHENNAI
KOLKATA
(iv) DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA
Q24. What are the possible outcome(s) from the following code?
import random
PICK=random.randint (1,3)
CITY= ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
for I in CITY:
for J in range (0, PICK):
print (I, end = "")
print ()
(i) |
(ii) |
(iii) |
(iv) |
DELHIDELHI |
DELHI |
DELHI |
DELHI |
MUMBAIMUMBAI |
DELHIMUMBAI |
MUMBAI |
MUMBAIMUMBAI |
CHENNAICHENNAI |
DELHIMUMBAICHENNAI |
CHENNAI |
KOLKATAKOLKATAKOLKATA |
KOLKATAKOLKATA |
KOLKATA |
|
Q25.What are the possible outcome(s) from the following code? Also, specify the maximum and minimum values that can be assigned to variable SEL.
import random
SEL=random. randint (1, 3)
ANIMAL = ["DEER", "Monkey", "COW", "Kangaroo"]
for A in ANIMAL:
for AA in range (0, SEL):
print (A, end ="")
print ()
(i) |
(ii) |
(iii) |
(iv) |
DEERDEER |
DEER |
DEER |
DEER |
MONKEYMONKEY |
DELHIMONKEY |
MONKEY |
MONKEYMONKEY |
COWCOW |
DELHIMONKEYCOW |
COW |
KANGAROOKANGAROOKANGAROO |
KANGAROOKANGAROO |
KANGAROO |
|
(iv) is the correct option.
Q26.What are the possible outcome(s) executed from the following code ?
import random
def main():
p = "MY PROGRAM"
i = 0
while p[i] != "R":
l = random.randint(0,3) + 5
print (p[l],end ="_")
i += 1
main()
(i) |
(ii) |
(iii) |
(iv) |
M_M_Y_P |
R_G_A_G |
G_G_R_O |
O_G_G_A |
Q27.Which is the incorrect outcome executed from the following code? Also, specify the maximum and minimum values that can be assigned to variable
x = 3
N = random.randint (1, x)
for i in range (N):
print (i, "#", i + 1)
(i) |
(ii) |
(iii) |
(iv) |
0#1 |
0#1 |
0#1 |
0#1 |
1#2 |
1#2 |
1#2 |
|
2#3 |
2#3 |
||
3#4 |
|
Ans. (iii) will not be printed
The maximum value of N is 3 and minimum is 1
Q28.Which is the incorrect outcome executed from the following code?
import random
movie_list = ['The Jungle Book', 'Finding Nemo', 'Harry Potter', 'The Incredibles', 'Frozen']
movie1 = random.choice(movie_list)
movie2 = random.choice(movie_list)
print ( movie1 ," or " ,movie2)
1. Finding Nemo or Harry Potter
2. Harry Potter or Toy Story
3. Frozen or The Jungle Book
4. Toy Story or Spider Man
Ans. (ii) and (iv) are incorrect option.
Q29.What are the possible outcome(s) executed from the following code ?
import random
color_list = ["Pink", "Red", "Sky blue", "Yellow", "Green"]
random.shuffle(color_list)
print ( color_list )
1. [‘Sky blue’, ‘Green’, ‘Yellow’, ‘Pink’, ‘Red’]
2. [‘Blue’, ‘Green’, ‘Pink’, ’Brown’, ‘Red’, ‘Black’]
3. [Yellow, Sky blue, Green, Pink, Red]
4. (‘Yellow’, ‘Red’, ‘Sky blue’, ‘Green’, ‘Pink’)
Ans. (i) , (iv) is a correct output
Q30.Which is the incorrect outcome(s) executed from the following code?
import random
list=[14, 62, 30, 57, 91]
str= ('Simply')
print ( random.choice(list),"and" , random.choice(str) )
1. 57 and i
2. 62 and S
3. 20 and p
4. 30 and M
Ans. (iii) and (iv) are incorrect options
Q31. 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(2, 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-
Ans. OUTPUT – (i) 30-40-50- Minimum value of BEGIN: 1 Minimum value of LAST:
Q37. 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 minimum and maximum values that
can be assigned to the variable End .
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&
Ans: (i) INDIGO&BLUE&GREEN&
Q38. What possible outputs(s) are expected to be displayed on screen at the time of execution of the program from the following code?
import random
X= random.random()
Y= random.randint(0,4)
print(int(),":",Y+int(X))
(i) 0:5 (ii) 0:3 (iii) 0:0 (iv) 2:5
Ans: (ii) and (iii)
(i) 100$$ 75$$ 10$$ |
(ii) 100$$
99$$ |
(iii)
150$$ 100$$ |
(iv) 125$$ 10$$ |
No comments:
Post a Comment