Create randomised integer values that add up to another integer

Hi guys

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.

That link seems overly complicated. Why don’t you just:

  1. Randomly generate a count.
  2. Randomly generate a list of numbers based on the randomly generated count.
  3. Sum the list of random numbers then calculate each one as a proportion of the fixed integer you’re interested in.
  4. Round the results to integers.

You could even cap the range of the randomly generated numbers so you can avoid really big or small values.

@bayowindapo ,

i am already dizzy … :wink: but what are you trying?


Should be solvable using Python and a while loop:

from random import randint

myNumber = 50
minRand  = 1
maxRand  = 5

numbList = []
checksum = 0

while checksum < myNumber:
    r = random.randint(minRand,maxRand)
    d = myNumber - checksum
    if r > d:
        numbList.append(d)
        checksum += d
    else:
        numbList.append(r)
        checksum += r

print(checksum)
print(numbList)

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 :wink:

Yes I figured I might have missed one case in the loop potentially (usually i use an exit variable if I brave a while loop).

Wanted to show them how it could be done in Python, despite my agreeimg with you that the use case is not entirely clear.

A solution with numpy (Python3), if duplicate numbers are allowed in the final list

sum to random 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!

Translate.dyn (73.1 KB)
ezgif.com-gif-maker