CBSE Class 12 Computer Science Question 53 of 62

Using Python Libraries — Question 6

Back to all questions
6
Question

Question 6

Suppose that after we import the random module, we define the following function called diff in a Python session :

def diff():
    x = random.random() - random.random() 
    return(x)

What would be the result if you now evaluate

y = diff()
print(y)

at the Python prompt ? Give reasons for your answer.

Answer
import random
def diff():
    x = random.random() - random.random() 
    return(x)

y = diff()
print(y)
Output
0.006054151450219258
-0.2927493777465524
Explanation

Output will be a floating-point number representing the difference between two random numbers. Since every call to random() function of random module generates a new number, hence the output will be different each time we run the code.