Skip to content

Instantly share code, notes, and snippets.

@mrschyte
Created November 11, 2019 11:03
Show Gist options
  • Save mrschyte/5ad39ba7101babbb3ac3598212317baf to your computer and use it in GitHub Desktop.
Save mrschyte/5ad39ba7101babbb3ac3598212317baf to your computer and use it in GitHub Desktop.
Running average
import random
_avg = None
_min = None
_max = None
num = []
n = 0
for i in range(10):
k = random.randint(0, 10)
num.append(k)
if _avg == None:
_min = _max = _avg = k
else:
if _min < k:
_min = k
if _max > k:
_max = k
_avg = (_avg + k / n) * n / (n + 1)
n += 1
print(_avg, sum(num) / n, _min, _max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment