A simple remedy
The reason why your algorithm produces desired sequences in a very low rate might be that you are generating random numbers that are so large on average that it is not easy for the sum of them to be smaller than the limit 100. A simple remedy is to change line 2) of your pseudocode to
2) generate random number between 1 and 55, let's say rand = 24
Here the upper limit, 55 is a number near 48 = 99//4 *2, where 4 = 5 - 1 is the number of numbers to be generated (it seems you require the first number must be 0) and 99//4 is about the average of each number. Then the algorithm will be much more likely succeed without going back to line 0). It is possible that the upper limit should be slightly bigger than 2 times the average to approximate the maximum rate of production. I profiled a few times so as to determine that 55 is the fastest number. You can experiment to find what is the best limit.
As successive differences
Here is another way to generate the desired sequences with length $m$ and sum less than $n$. It works well if $n$ is not large, in which case it does not take long to complete the sorting step.
- Generate $m$ random numbers between 0 inclusive and $n$ exclusive.
- Sort them so we obtain $a\le a\le\cdots\le a[m-1]$.
- Return sequence $b, b, \cdots, b[m-1]$, where $b=a$ and $b[i]=b[i]-a[i-1]$ for all $i\gt1$.
Scaling technique, the fastest way
- Generate m random numbers between 0 inclusive and 1, a, a, ..., a[m-1].
- Let s be the sum of them. Compute the scaling factor f = u/s, where u is the given upper limit of the sum.
- Let b[i] be a[i] multiplied by f, chopping off the fraction part.
- Return sequence b, b, ..., b[m-1].
You can also try generating m + k of them, following by the same scaling step, but returning only the first m numbers. You can either fix k or even let k be random.
Note that different algorithms generate sequences with different "randomness", which may or may not be significant for your purpose.
You can search for how to generate random sequences with given sum for more methods. If you prefer sum to be less than a given limit, just generate a longer sequence with a sum that is bigger than the given limit. Then take the first m numbers.