Computer Science

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.

Python Libraries

6 Likes

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.

Answered By

2 Likes


Related Questions