0% found this document useful (0 votes)
7 views

Random Function

The document discusses various random functions in Python, including random(), randrange(), and randint(), explaining their usage and expected outputs. It provides code snippets and asks for possible outputs along with maximum and minimum values for certain variables. The document contains multiple practice questions to test understanding of random number generation in Python.

Uploaded by

prafulla gouda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Random Function

The document discusses various random functions in Python, including random(), randrange(), and randint(), explaining their usage and expected outputs. It provides code snippets and asks for possible outputs along with maximum and minimum values for certain variables. The document contains multiple practice questions to test understanding of random number generation in Python.

Uploaded by

prafulla gouda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Options:

Random Number (i) 10#40#70#


When you require such numbers that are not (ii) 30#40#50#
known earlier, e.g., CAPTCHA or any type of (iii) 50#60#70#
serial numbers, you can use random functions. To (iv) 40#50#70#
use these functions, we need to import the random
module.
NO-2
1. random() → random() generates a
floating-point value between 0 and 1 What possible output(s) are expected to be
(including 0 but excluding 1). It does displayed on the screen at the time of execution of
not require any argument. the program from the following code? Also,
specify the maximum values that can be assigned
>>> import random to each of the variables Lower and Upper.
>>> random.random()
>>> 0.9204919856789 import random
AR = [20, 30, 40, 50, 60, 70]
2. randrange() → randrange() returns Lower = random.randint(1, 4)
an integer value between the given Upper = random.randint(2, 5)
lower and upper limits, excluding the
upper limit. for K in range(Lower, Upper + 1):
print(AR[K], end="#")
>>> import random
>>> random.randrange(2, 6) # Add a new line for better formatting
>>> 3 print()
>>> 5
Options:
3. randint() → randint() takes two (i) 40#
parameters, a (lower limit) and b (ii) 40#50#60#
(upper limit). It returns an integer value (iii) 50#
between the given lower and upper (iv) All of these
limits, including the upper limit.
NO-3. What possible output(s) are
>>> import random
>>> random.randint(2, 6)
expected to be displayed on the screen at
>>> 3 the time of execution of the program?
>>> 6 import random
print(random.randint(15, 25), end=' ')
print((100) + random.randint(15, 25),
end=' ')
Practice Questions print((100) - random.randint(15, 25),
NO-1 end=' ')
print((100) * random.randint(15, 25))
What possible output(s) are expected to be
displayed on the screen at the time of execution of (i) 15 122 84 2500
the program from the following code? Also, (ii) 21 120 76 1500
specify the maximum values that can be assigned (iii) 105 107 105 1800
to each of the variables FROM and TO. (iv) 110 105 105 1900

import random
AR = [20, 30, 40, 50, 60, 70]
FROM = random.randint(1, 3) NO-4. What possible output(s) are
TO = random.randint(2, 4) expected to be displayed on the screen at
the time of execution of the program?
for K in range(FROM, TO + 1):
print(AR[K], end="# ") P = 7
import random as r
val = 35
Page 1 of 3
Num = 0 import random
for i in range(1, 5): L = [10, 7, 21]
Num = val + r.randint(0, P - 1) X = random.randint(1, 2)
print(Num, " $ ", end="") for i in range(X):
P = P - 1 Y = random.randint(0, X - 1)
print(L[Y], "$", end=" ")
(a) 41 38 38 37
(b) 38 40 37 34 (i) 10 7
(c) 36 35 42 37 (ii) 21 7
(d) 40 37 39 35 (iii) 21 10
(iv) 7 $

NO-5. What is the minimum and


maximum value of c in the following NO-8. Observe the following Python
code snippet? code and find out which of the given
import random options (i to iv) are the expected correct
a = random.randint(3, 5) output(s).
b = random.randint(2, 3)
c = a + b Also, assign the maximum and minimum value
print(c) that can be assigned to the variable Go.

(a) 3, 5 import random


(b) 5, 8 X = [100, 75, 10, 125]
Go = random.randint(0, 3)
(c) 2, 3
for i in range(Go):
(d) 3, 3 print(X[i], "$$", end=" ")

(i) 1007510
NO-6. What possible outputs are (ii) 7510125
expected to be displayed on the screen at (𝑖𝑖𝑖)7510
the time of execution of the program?
(𝑖𝑣)10125$$100
Also, specify the maximum value that can be
assigned to each of the variables L and U.
import random NO-9. Study the following program and select the
Arr = [10, 30, 40, 50, 70, 90, 100] possible output(s) from the options (i) to (iv)
L = random.randrange(1, 3) following it. Also, write the maximum and the
U = random.randrange(3, 6) minimum values that can be assigned to the
for i in range(L, U + 1): variable Y.
print(Arr[i], "@", end="")
import random
(i) 40 @50 @ X = random.random()
(ii) 10 @50 @70 @90 @ Y = random.randint(0, 4)
(iii) 40 @50 @70 @90 @ print(int(X), ":", Y + int(X))
(iv) 40 @100 @
(i) 0 : 0
(ii) 1 : 6
(iii) 2 : 4
NO-7. What possible output(s) are (iv) 0 : 3
expected to be displayed on the screen at
the time of execution of the following
code? NO-10.
Study the following program and determine the
Also, specify the maximum and minimum value possible output(s) that may be displayed on the
that can be assigned to the variable X. screen when executed. Also, specify the minimum
Page 2 of 3
and maximum values that can be assigned to the can be assigned to the variable R when K is
variable NUMBER. assigned the value as 2.
import random import random
STRING = "CBSEONLINE" Signal = ['stop', 'Wait', 'Go']
NUMBER = random.randint(0, 3) for K in range(2, 0, -1):
N = 9 R = random.randrange(K)
while STRING[N] != "L": print(Signal[R], end='#')
print(STRING[N] + STRING[NUMBER] +
"#", end=" ")
NUMBER = NUMBER + 1
N = N - 1

(i) ES#NE#IO#
(ii) LE#NO#ON#
(iii) NS#IE#LO#
(iv) EC#NB#IS# NO-16.
What possible output(s) are expected to be
displayed on the screen at the time of execution of
NO-11. the program from the following code? Also,
Study the following program and determine the specify the minimum and maximum values that
possible output(s) that may be displayed on can be assigned to the variable End.
execution. import random
import random Colours = ["VIOLET", "INDIGO", "BLUE",
PICK = random.randint(0, 3) "GREEN", "YELLOW"]
CITY = ["DELHI", "MUMBAI", "CHENNAI", End = random.randrange(2) + 3
"KOLKATA"] Begin = random.randrange(End) + 1
for I in CITY: for i in range(Begin, End):
for j in range(1, PICK): print(Colours[i], end="&")
print(I, end=" ")
print()

(i)
DELHIDELHI MUMBAIMUMBAI
CHENNAICHENNAI KOLKATAKOLKATA
(ii)
DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI
(iii)
DELHI MUMBAI CHENNAI KOLKATA
(iv)
DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA

NO-15.
What possible output(s) is/are expected to be
displayed on the screen at the time of execution of
the program from the following code? Also,
specify the maximum and minimum values that

Page 3 of 3

You might also like