Skip to content

Instantly share code, notes, and snippets.

@antiface
Last active April 23, 2020 08:20
Finding the mean of an array of size n of random values between 0 and 1.
# Finding the mean of an array of size n,
# an array of random values (reals) between 0 and 1 ("probabilities")
# A.G. (c) 2020. All Rights Reserved.
# random.random() generates a float value between 0 and 1;
# The sum is of a list generated through list comprehension;
# The list of random values is of size n;
# We divide the sum of the list by n to get the mean.
f = lambda n: (sum([random.random() for i in range(n)]))/n
"""
>>> f(10)
0.6641219441203049
>>> f(100)
0.5384967134143757
>>> f(1000)
0.5022625876708935
>>> f(10000)
0.5040273099975735
>>> f(100000)
0.499092114558579
>>> f(1000000)
0.5000853350917197
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment