Search code examples
pythonnumbersmean

random float numbers and their mean and standart deviation


how to get a list of 1000 random float numbers without dublicates and find their mean value in python?

import random
rnd_number=random.random()
def a():
        l=[]
        m=1
        for i in range(1000):
                l.append(rnd_number)
        return l
        for i in l:
                m=m+i
        return m//b
print (a())

i am probably wrong with returning l before the other operation but when the code works there are 1000 of the same float numbers on the screen


Solution

  • Hope this would help!

    import numpy as np
    import random
    
    N=10
    # number of digits you want after the .dot sign
    # more digits .dot ensure mo duplication
    Npts=1000
    # number of random numbers
    num=[]
    # initialize the array
    for m in range(1000):
        nm=round(random.random(), N)
        # local variable to take the random numbers
        num.append(nm)
        # append the random number nm to the original num array
    print(num)
    # printing the random numbers
    
    # Let's check is there is a duplication or not
    my_num=set(num)
    if len(num)!=len(my_num):
        print("Duplication in the array")
    else:
        print("NO duplication in the array")
    
    # Calculating mean
    avg=sum(num)/Npts
    print(avg)