187k views
2 votes
The function random( ) returns a randomly chosen floating point value between 0.0 and 1.0. Write a program that repeatedly calls the random( ) function, stopping only when it receives a value that differs from a previously drawn value by at most 0.05. Your program should print the total number of calls that were made.

1 Answer

6 votes

Answer:

See Explaination

Step-by-step explanation:

a and b two variables contains the random numbers.

a contain the previous number and b contains the current number .

count variable counts the number of calls to random function.

while loop is run until the absolute difference between a and b is greater than 0.05

Code:

from random import random

a = random()

b = random()

count = 2

print('{:.2f}'.format(a))

print('{:.2f}'.format(b))

while(abs(a-b)>0.05):

a = b

b = random()

count = count+1

print('{:.2f}'.format(b))

print('Total of', count, 'calls were made.')

User CShark
by
3.2k points