Random No Generator

import random
a=int(input(‘Enter the no:’))
for x in range(a):
f.write(str(number)+ ‘\n’)

Above code generate only single value whereas this should be generating 10 random numbers. Please correct me where I am doing mistake and guide me accordingly.


Hi Azeem,

Each time through the loop, you open the file, write one number
over-writing the number you had before, then close the file again.

This is probably the simplest way to fix it.

Open the file once, write the ten numbers, then close:

with open('rn.txt', 'w') as f:
    for i in range(10):
        number = random.random()
        f.write(str(number) + '\n')

# Now read the file and see what's there:
with open('rn.txt',	'r') as f: