Random Module
Random Module
randrange( ) :
This function generates a random number between the lower and upper limit
(including lower limit and excluding upper limit). for example the following code will
generate a random number less than 20 :
import random
a=random.randrange(20)
print(a)
The above code will generate any number from 0 to 19.
NOTE :
1. By default the lower limit of randrange function is 0.
2. randrange( ) always generate a number one less than the higher limit.
Q2. Write the code to generate a random number between 7 and 17 (both inclusive)
Ans.
import random
a=random.randrange(7, 18)
print(a)
Q3. Write the code which always generate 0 number by using randrange( ).
Ans.
import random
a=random.randrange(1)
print(a)
Q4. Write the code to select random names from the given list of names.
import random
name=["Amit","Sumit","Naina","Suman","Parth"]
rd=random.randrange(______________)
print(name[rd])
What minimum number should we fill in the above blank so that it will print any
name from the given list. (Answer is 5)
random():
This function generates a random floating point number from 0 to 1(including 0 and
excluding 1) like 0.15895645215423562.
Syntax is :
random.random()
Q5. Write code to generate any numbers between 10 and 15(including both)
import random
print(int(random.random( )*6+10))
Q6. Write the code to generate random three digit number using random in python.
import random
print(int(random.random( )*900+100))
Q7. Write the code to generate random two digit number using random in python.
import random
print(int(random.random( )*90+10))
randint() :
This function generates a random number between the lower and upper
limit (including both the limits).
Syntax of randint() is
a = random.randint(L,U) # where L is the lower limit and U is the upper limit.
The value of a will be in the range ( L<=a<=U)
Q8. Write a program to generate a random number between 20 and 30(including both)
import random
print(random.randint(20,30))
Q9. Write a program which adds any random five even numbers in a list that falls
between the highest and the lowest number.(Both highest and lowest numbers are
accepted from the user.)
import random
L=[]
h = int(input("Enter the highest limit"))
l=int(input("Enter the lowest limit"))
while(i):
a=random.randrange(l+1,h) #we are not including upper limit and lower limit
if(a%2==0):
L.append(a)
if(len(L)==5):
break
print(L)
Quiz time
Q1. Write the output of the following code :
import random
for i in range(random.randint(2,2)):
print(i)
0
1
1
2
0
1
2
import random
for i in range(random.randint(2,3)):
print("A")
A
A
A
A
A
A
A
OR
A
A
A
ERROR
Q3. What is the minimum and maximum value of 'A' in the following statement..
import random
A=random.randint(2,30)
print(A)
3 and 30
2 and 30
2 and 29
Always generate 0
Always generate 1
Shows Error
Q7. Which of the following subject will never printed in following code?
import random
L=["English","Hindi","Math","Science","SST","CS","IP"]
for i in range(random.randint(1,6)):
print(L[i])
CS and IP both
English
IP
Q8. What is the minimum and maximum value of 'v' in the following statement.
import random
v=random.randrange(20)-3
print(v)
0,19
0,17
-3,16
-3,17
Shuffle( )
randrange( )
randomrange( )
uniform( )
Q10. A = random.______([1,2,3,4,5])
Value of A can be 1 to 5(including both)
shuffle
choice
uniform
none of these