What could be the possible outputs of the following code from the given choices? Why?

import math

import random

a = (math.random()*4) + 10

b = (math.random()*4) + 10

print (a, b)

(i) 0.0101 4.5623

(ii) 10.5678 13.9234

(iii) 11.1234 12.7826

(iv) 0.9238 14.7895

 

Answer:

(ii) and (iii) are the possible outputs because:

  • The range  of random( ) is 0.0 to 1.0.
  • When the output is  multiplied with 4 the range becomes 0.0 to 4.0 
  • When 10 is added the range becomes 10.0 to  14.0.
  • So the values of a and b should be between 10.0 to 14.0(where 14.0 is exclusive).
Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.