(d) 48#
Important Functions of Random
Q2.
Module import random
1. random() S = "COMPUTER"
Returns a random floating-point number between n = [Link](1, 4)
0.0 and 1.0. for i in range(n):
import random
print([Link]()) print(S[i], end="*")
Example Output: 0.6789 (a) C*
2. randint(a, b) (b) C*O*
Returns a random integer between a and b (both (c) C*O*M*
inclusive). (d) C*O*M*P*U*
print([Link](1, 10)) Q3.
Example Output: 7 import random
3. randrange(start, stop, step) L = [10, 20, 30, 40, 50]
Returns a random number from the specified range. x = [Link](1, 3)
print([Link](10, 50, 5))
for i in range(x, x+2):
Possible Output: 20
print(L[i], end="@")
4. choice(sequence) (a) 20@30@
Returns a random element from a list, tuple or
(b) 30@40@
string.
colors = ["Red", "Blue", "Green"] (c) 40@50@
print([Link](colors)) (d) 10@20@
5. shuffle(sequence) Q4.
Randomly rearranges the elements of a list. import random
numbers = [1, 2, 3, 4, 5]
a = "PYTHON"
[Link](numbers)
print(numbers) n = [Link](0, 5)
Note: shuffle() works only with mutable print(a[n], end="#")
sequences like lists. (a) P#
6. uniform(a, b) (b) H#
Returns a random floating-point number between a (c) N#
and b. (d) A#
print([Link](1.5, 5.5))
Q5.
import random
Class 12 Computer Science (Python) L = ["Red", "Blue", "Green", "Yellow"]
[Link](L)
Random Module - Output Based Questions
for i in L:
CBSE Board Pattern Practice Worksheet print(i, end=" ")
Instructions: Identify all possible outputs for each (a) Output order remains unchanged.
question. (b) Output order is always alphabetical.
Q1. (c) Elements appear in random order.
import random (d) Program generates an error.
L = [12, 24, 36, 48]
Q6.
a = [Link](0, 1)
import random
b = [Link](2, 3)
x = [Link](2, 12, 3)
for i in range(a, b):
print(x)
print(L[i], end="#")
(a) 2
(a) 12#24#
(b) 5
(b) 24#36#
(c) 8
(c) 12#24#36#
(d) 12
Q7. Q12.
import random import random
L = [5, 15, 25, 35] L = [11, 22, 33, 44, 55]
a = [Link](L) n = [Link](1, 4)
print(a + 5) for i in range(n):
(a) 10 print(L[i], end="*")
(b) 20 (a) 11*
(c) 30 (b) 11*22*
(d) 50 (c) 11*22*33*
Q8. (d) 22*33*
import random Q13.
S = "SCIENCE" import random
x = [Link](1, 5) x = [Link](1, 5)
for i in range(0, x, 2): print(round(x, 1))
print(S[i], end="#") (a) Output is an integer between 1 and 5.
(a) S# (b) Output is a floating-point number between
(b) S#I# 1.0 and 5.0.
(c) S#I#N# (c) Output is always 5.0.
(d) C# (d) Program generates an error.
Q9. Q14.
import random import random
L = [2, 4, 6, 8, 10] L = [100, 200, 300, 400]
p = [Link](2, 4) a = [Link](0, 2)
print(L[p-1], end="@") print(L[a+1], end="#")
(a) 4@ (a) 100#
(b) 6@ (b) 200#
(c) 8@ (c) 300#
(d) 10@ (d) 400#
Q10. Q15.
import random import random
a = [Link](1, 3) S = "BOARD"
b = [Link](3, 5) x = [Link](1, 5)
for i in range(a, b): for i in range(x):
print(i, end="#") print(S[-1], end="*")
(a) 1#2# (a) D*
(b) 2#3#4# (b) D*D*
(c) 3#4# (c) D*D*D*
(d) 1#2#3#4#5# (d) D*D*D*D*D*
Q11.
import random
S = "KNOWLEDGE"
x = [Link](S)
print(x)
(a) K
(b) W
(c) E
(d) Z