Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello, this program is related to the mean and standard deviation, the mean I got, but when trying to do it for the standard deviation I was not successful, I tried in different ways, I couldn't use the same numbers of the mean, because when calling the def function draw() generated new numbers.

What I have tried:

Python
from random import *

soma = 0.0
 


def sorteio():
    a = randint(0,5)
    return a

for i in range(0, 6):
    
    soma += sorteio()

media = soma / 5    
print('media = ',media)
Posted
Updated 19-Jul-22 20:20pm
v2
Comments
Patrice T 19-Jul-22 23:40pm    
Show your attempt to standard deviation.
Here we help you to fix your code.

If you need to use the same numbers - and you do - then generate the randoms numbers and store them in an array: Python Arrays[^]
You them modify both the mean and standard deviation code to use the data from the array instead of fetching a new random number each time.
 
Share this answer
 
Comments
Maciej Los 20-Jul-22 1:23am    
Short And To The Point!
CPallini 20-Jul-22 2:20am    
5.
Note: your computation of the average is NOT correct:
Python
for i in range(0, 6):
    soma += sorteio()
extracts (and sums up) 6 values, while
Python
media = soma / 5
divides by 5.


Anyway, as already suggested by Griff, consider rearranging your code for keeping track of the extracted values, e.g.
Python
from random import *
  
def sorteio():
    a = randint(0,5)
    return a

def average(l):
    sum = 0.0
    for x in l:
        sum = sum + x
    return sum/len(l)

l = [] # the list will contain the random extracted numbers
for i in range(0, 5):
    l.append(sorteio())

print(l) # show the list
print("average ", average(l))

Then, standard deviation implementation should be straightforward.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900