0% found this document useful (0 votes)
3 views16 pages

Random-2

Uploaded by

pranayinfo2023
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Random-2

Uploaded by

pranayinfo2023
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

for more updates visit: www.python4csip.

com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

RANDOM NUMBERS
for more updates visit: [Link]

Using Random Module


 Python has a module namely random that provides
random – number generators. Random number means
any number generated within the given range.
 To generate random number in Python we have to
import random module
 3 most common method to generate random number in
python are :
 random() function
 randint(a,b) function
 randrange(a,b) function

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

random() function
 It is floating point random number generator
between 0.0 to 1.0. here lower limit is inclusive
where as upper limit is less than 1.0.
 0<=N<1
 Examples:

Output is less than 1


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

random() function
 To generate random number between given range
of values using random(), the following format
should be used:
 Lower_range + random() * (upper_range-lower_range)
 For example to generate number between 10 to 50:
 10 + random() * (40)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

randint() function
 Another way to generate random number is
randint() function, but it generate integer numbers.
 Both the given range values are inclusive i.e. if we
generate random number as :
 randint(20,70)
 Inabove example random number between 20 to 70 will
be taken. (including 20 and 70 also)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

E
X
A
M
P
L
E

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

O
U
T
P
U
T

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Just a Minute…
 Give the following python code, which is repeated
four times. What could be the possible set of
output(s) out of four sets (ddd is any combination of
digits)
import random
print(15 + [Link]()*5)
a) b) c) d)
[Link] [Link] [Link] [Link]
[Link] [Link] [Link] [Link]
[Link] [Link] [Link] [Link]
[Link] [Link] [Link] [Link]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Just a Minute…
 What could be the minimum possible and maximum
possible numbers by following code
import random
print([Link](3,10)-3)
 In a school fest, three randomly chosen students out
of 100 students (having roll number 1 -100) have to
present the bouquet to the guests. Help the school
authorities choose three students randomly

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Just a Minute…

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
Just a Minute…
for more updates visit: [Link]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

Look at the following Python code and find 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 PICKER.
Note:
‐ Assume all the required header files are already being included in the code.
‐ The function randint() generates an integer between 1 to n
import random
PICKER=1+[Link](0,2)
COLOR=[”BLUE”,”PINK”,”GREEN”,”RED”]
for I in range(1,PICKER+1):
for j in range(I+1):
print(COLOR[j],end=‘’)
print()
for more updates visit: [Link]

What are the possible outcome(s) executed from the following


code? Also specify the maximum and minimum values that
can be assigned to variable PICK

1) 2)
DELHIDELHI DELHI
MUMBAIMUMBAI DELHIMUMBAI
CHENNAICHENNAI DELHIMUMBAICHENNAI
KOLKATAKOLKATA
3) 4)
DELHI DELHI
MUMBAI DELHIMUMBAI
CHENNAI KOLKATAKOLKATAKOLKATA
KOKLATA
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

randrange() function
 This function is also used to generate random
number within given range.
 Syntax
 randrange(start,stop,step)

It will generate random


number between 5 to 14

random output between 5 to 14, may vary

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

randrange() function

It will generate
random number
between 1 to 29 with
stepping of 2 i.e. it
will generate number
with gap of 2 i.e.
1,3,5,7 and so on

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Mathematics Game for Kids

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Mathematics Game for Kids

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like