This is from my own question on Stack Overflow.
I want to get average numbers from a range such that they fit an intended average. I expect something bell-curve-like, but asymmetrical unless the average is the mean of the range. The function should allow for any degree of deviation. In my Overflow question, I wanted it to pick from a list, but that's an easy modification if my idea works.
This is what I've come up with: (imports: numpy and random)
def randlist(minn, maxn, goaln, countn):
nlist = []
for i in range(0, countn):
if len(nlist) > 10:
ave = numpy.mean(nlist)
if ave > goaln:
a = random.uniform(minn, goaln)
else:
a = random.uniform(goaln, maxn)
else:
a = random.uniform(minn, maxn)
nlist.append(a)
return nlist
Trying it out:
b = randlist(1, 10, 7, 1000)
print len(b)
>> 1000
print numpy.mean(b)
>> 6.99951157861
I'm no mathematician; is this as functional as it appears to me to be?
random.betavariate
? \$\endgroup\$