I’ve been trying to figure out how to add a randomised number of integer values to a set number of lists. The sum of these integer values should add up to another integer.
The end goal is to use these numbers to translate a list of surfaces vertically, but the number of surfaces translated has to be equal to a particular integer value. The integer value will never be less than the total number of initial surfaces being translated.
I tried using the tread in the link below.
Thanks for your help.
If the remainder number never gets generated this code will get stuck in a never ending loop. You’re essentially doing what I suggested but aimlessly trying to stay true to the OP request…but you’re dealing with random numbers, so why bother trying to fit random numbers into a known datum? Just generate a random count of random numbers then fit that into your target number proportionally. No need for a while loop, conditionals, nor does it rely on chance for the process to complete, and its just as ‘random’ as what you are doing
A solution with numpy (Python3), if duplicate numbers are allowed in the final list
import sys
import clr
clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')
import numpy as np
def sum_to_list(parts : int, total : int) -> list:
"""
-description:
generate a list of integer that sum is equal to 'total'
-Parameters:
parts : number of items in return list
total : sum of the return list
-Return:
list
"""
array = np.random.multinomial(total, np.ones(parts) / parts)
return array.tolist()
OUT = sum_to_list(IN[0], IN[1])
Thanks all for the responses.
I learned a lot here. I think I was not clear enough as to what my end goal was.
The end goal is to run generative design massing studies which take varied height restrictions along a site into account.
To do this, I would need to be able to translate the base surfaces at random heights and find out if they intersect with the height restriction. I got @Thomas_Mahon suggestion to work using OOTB nodes.
Please let me know if there is a better way to achieve this.
Thanks for your help.
See the script attached for anyone interested.
Cheers!