Algorithm for generating random incrementing numbers up to a limit I'm trying to write a code to generate incremental sequences of numbers such as:

0 + 1 + 3 + 5 + 1 = 9 
0 + 5 + 1 + 1 + 1 = 8 
0 + 1 + 1 + 2 + 1 = 5


I have 3 constraints: 

1) I need to have limited number of addends (here it is =5)

2) final sum must be smaller than certain limit ( here limit is &lt;9)

3) addends must be random

As for now, I generate sequences randomly and select only suitable ones. For 2-digit numbers and for long sequences (&gt;8) my algorithm takes significant time.

Is there a better algorithm for such problem? 

As least could you tell me, what branch of CS is studying such problems?

UPDATE (algorithm):

0) array = [0,]; // initial array
1) if sum(array) &gt; 99, go to 6)
2) generate random number in [1..99], let's say rand = 24
3) rand = array[-1] + rand // add random number to last value of array
4) array.push(rand) // add the random number to array
5) goto 1)
6) if length(array) &lt; 5, goto 0) // 5 is desired sequence length
-- generate ALL sequences of n natural numbers with given sum
-- should be called with n&lt;=sum
gen(n,sum):
  for i in 1..sum-n+1:
    -- next number in sequence is i
    gen(n-1,sum-i)  -- generate ALL possible sequences of remaining numbers