15,957,045 members
1.00/5 (1 vote)
See more: , +
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
Patrice T 19-Jul-22 23:40pm
Show your attempt to standard deviation.

## Solution 1

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.

Maciej Los 20-Jul-22 1:23am
Short And To The Point!
CPallini 20-Jul-22 2:20am
5.

## Solution 2

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.