random float numbers and their mean and standart deviation

Question:

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

Asked By: Sevda Niftaliyeva

||

Answers:

If you want to have distinct random numbers, you have to draw number on every loop iteration. To avoid duplicates you can use set, which stores unique values.

import random

def a():
    mySet = set()
    while len(mySet) < 1000:
        mySet.add(random.random())
    return mySet

print(a())
Answered By: mumiakk

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)
Answered By: MM-Borhan
import random
def a():
        s=0
        m=0
        l=[]
        for i in range(1000):
                rnd_number=random.random()
                l.append(rnd_number)

        for n in l:
                m=m+n
        m=m/len(l)
        for k in l:
                s=s+(k-m)**2
        s=(s/len(l))**(1/2)
        
        return l,m,s
print (a())

i did like this and got the right answers for both the mean and standart devaiation of 1000 random float numbers
(i checked it with 4 actually but i think it is gonna work for 1000 too)

Answered By: Sevda Niftaliyeva
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.