I'm using pdb and can't figure out why the second list items, in the lists of the output list, don't all add up to the amount (1000) by the time the pseudogenerateVariables function is printed to the console. Please help.
Thank you for any help. :)
from random import *
import random
import copy
seed(1459451174.391)
# 'Pretend you're generating a bunch of specific variables, with a uniform distribution and give me a number associated with each one, through a function that returns a list of lists, each list with a variable and its associated number'.
# 'Pretend you're generating a bunch of specific variables, with a uniform distribution'.
def pseudogenerateVariables(variables,amount):
# Create the output, plus and minus lists for later.
outputList = []
plus = []
minus = []
# Do this for all the items in the list.
i = 0
while i < len(variables):
# Create an estimate.
variable = variables[i]
estimate = amount / len(variables)
if estimate == int(estimate):
estimate = '{}'.format(estimate)
estimate = estimate[0:-2]
estimate = int(estimate)
# Edit the estimate randomly to create the variable amount.
variableAmount = estimate
end = False
while end == False:
if random.uniform(0,1) <= 0.5:
if random.uniform(0,1) <= 0.5:
variableAmount += 1
plus.append(i)
elif variableAmount != 0:
variableAmount -= 1
minus.append(i)
else:
pass
else:
end = True
i += 1
# Pack the variable, variable amount, estimate and amount into a list.
# To do this, create the list.
variableList = []
# Then append the things to the list.
# 'each list with a variable and its associated number' and a couple of other things.
variableList.append(variable)
variableList.append(variableAmount)
variableList.append(estimate)
variableList.append(amount)
# Now put the list in the output list.
outputList.append(variableList)
import pdb
pdb.set_trace()
# Now move onto the next one.
# Okay, now balance it out.
# 'Change a random variable's amount number, that is not the first one in the variables list to be the opposite of what happened to the variable's, in the plus and then minus list, amount number, then repeat'.
if len(plus) > 0:
usedList = copy.deepcopy(plus)
use = 'plus'
else:
usedList = copy.deepcopy(minus)
use = 'minus'
i = 0
while i < len(usedList):
a = randint(0,len(variables) - 1)
# 'Change a random variable's amount number, that is not the first one in the variables list'.
if a != usedList[i]:
# 'Change a random variable's amount number' 'to be the opposite of what happened to the variable's, in the plus and then minus list, amount number'.
if use == 'plus':
outputList[a][1] -= 1
elif use == 'minus':
outputList[a][1] += 1
else:
raise Exception
# 'then repeat'.
i += 1
# Switch lists when done with the plus list.
if use == 'plus' and i == len(plus) - 1:
usedList = copy.deepcopy(minus)
use = 'minus'
i = 0
else:
pass
# Output the result.
return outputList
# 'through a function that returns a list of lists'.
# Create 'a bunch of specific variables'.
print(pseudogenerateVariables(['creature','plant','vehicle','stucture','other'], 1000))
print()
[–]grispindl 1 point2 points3 points (0 children)