Class 12 Computer Science Important Concept of Module Random in Python

Share with others

Random in Python
Random in Python

Random in Python :

When ever there is a situation where we need to generate random numbers in coding of python, then python allow us to generate random numbers by using module RANDOM in Python.

Following are the situations where we need to generate random numbers in Python

  1. To generate the scratch card of online Lottery.
  2. To generate captcha code.
  3. To generate OTP (One Time Password)
  4. Computer games like LUDO (where need to generate random number between 1 to 6)

Functions in random Module

There are many functions in random module in python. Let we start

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.

Q1. Write the code to generate a number less than 10.

Ans.
import random
a=random.randrange(10)
print(a)

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()

random( ) function takes no argument.

NOTE : It always generate floating point number between 0 and 1(including 0 and excluding 1)

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)

uniform()

This function is used to generate a random floating point number between the lower limit and upper limit (excluding both).

Q10. Write the code to generate random floating point number between 10 and 20.

import random
print(random.uniform(10,20))

Output :
12.123199345853024 (It could be any other number)

choice() :

This function is used to select any random element from any sequence like any list, tuple or dictionary.

Q11. Write the code to select any random element from given list.

import random
L = [30,40,50,60,10,90]
print(random.choice(L))

Output :
40(It could be any other element from the list)

Q12. Write the code to select any random element from given tuple.

import random
L = (30,40,50,60,10,90)
print(random.choice(L))

Output :
40(It could be any other element from the list)

shuffle():

This function is used to shuffle(change the sequence) the elements in the list.

Syntax is :
random.shuffle(List)

Q13. Write a program to shuffle the elements of given list

import random
r=["Amit","Sumit","Naina","Suman","Parth"]
random.shuffle(r)
print(r)

Output: 
['Suman', 'Naina', 'Sumit', 'Amit', 'Parth']

Random in Python
Random in Python

Fill in the blank (Random in Python)

  1. _________ module is to be imported to use randrange() function.
  2. ___________ and _________ functions of random module return floating point random number.
  3. A function which generates a floating point number between 0 and 1 is _____________________
  4. __________ and _________ functions of random module return random integers.
  5. ________________ function of random module takes no argument.
  6. ________________ function change the sequence of elements in the list.

Random in Python
Random in Python

True/False (Random in Python)

  1. random() function generates integers.
  2. random.randrange(2,20) will always generate number greater than 2 and less than 20.
  3. random.random()*900 + 100 always generate 3 digits number.
  4. shuffle() function can not work on tuple.
  5. uniform() function always generate floating point numbers.

Random in Python
Random in Python

TOTAL MCQ – 10

TOTAL MARKS – 10

CLICK TO START QUIZ -#1



Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

Leave a Reply

%d bloggers like this: