Let's analyse the problem:
We will be generating 14 numbers and adding the 15th manually to get the correct sum.
But I would change @jfaccioni's approach of generating all numbers at once and restart the loop earlier: e.g. if we generate 5
5 times, we already know we can't get the correct sum! (5*5+20*(15-5) < 227)
def rng_list():
while True:
rng_list = []
current_sum = 0
for i in range(14):
r = random.randint(5, 20)
rng_list.append(r)
current_sum+= r
if not 5*(14-i) <= 227-current_sum <= 20*(14-i): #`i` goes from 0 to 14, so 14-i is how many (from 15) numbers are still not calculated
break
if len(rng_list) == 14:
rng_list.append(227-current_sum)
return rng_list
print("Pass failed with list {}, sum {} - trying again.".format(rng_list, current_sum)) #added to debug/show how it works
I've added the print to monitor how it works when writing, you can change it to a counter how many times you needed to re-roll your list. But right now you can also see how failed attempts are of different length, as we monitor the current sum every time!
>>> rng_list()
Pass failed with list [12, 7, 18, 20, 12, 12, 15, 7, 15, 16, 7], sum 141 - trying again.
Pass failed with list [17, 6, 20, 5, 16, 18, 5, 19, 19, 7, 8], sum 140 - trying again.
Pass failed with list [10, 15, 18, 10, 8, 8, 12, 12, 13], sum 106 - trying again.
Pass failed with list [9, 12, 8, 5, 17, 20, 20, 6, 8], sum 105 - trying again.
Pass failed with list [9, 16, 9, 16, 6, 17, 20, 15, 9, 11, 15], sum 143 - trying again.
Pass failed with list [5, 14, 13, 12, 12, 13, 13, 9, 8], sum 99 - trying again.
Pass failed with list [11, 9, 5, 11, 11, 13, 18, 7], sum 85 - trying again.
Pass failed with list [10, 12, 19, 9, 14, 16, 11, 19, 5, 5], sum 120 - trying again.
Pass failed with list [11, 10, 8, 10, 10, 17, 17, 9, 19, 14], sum 125 - trying again.
Pass failed with list [13, 11, 7, 15, 14, 7, 5, 10], sum 82 - trying again.
Pass failed with list [11, 6, 7, 20, 6, 17, 18, 12, 8], sum 105 - trying again.
Pass failed with list [16, 17, 9, 18, 7, 8, 17, 14, 13, 13, 14], sum 146 - trying again.
[19, 6, 18, 9, 19, 20, 15, 14, 16, 15, 11, 18, 11, 20, 16]
>>> rng_list()
Pass failed with list [17, 6, 8, 9, 14, 17, 13, 8, 10], sum 102 - trying again.
Pass failed with list [7, 16, 12, 8, 20, 19, 18, 15, 5, 5], sum 125 - trying again.
Pass failed with list [5, 7, 6, 15, 12, 17, 6, 10], sum 78 - trying again.
Pass failed with list [17, 18, 8, 17, 18, 6, 10, 16, 18, 18, 6, 5], sum 157 - trying again.
Pass failed with list [10, 13, 9, 11, 11, 5, 18, 17, 13, 12], sum 119 - trying again.
Pass failed with list [20, 8, 8, 7, 14, 16, 17, 15, 15, 19, 13, 15, 17], sum 184 - trying again.
Pass failed with list [19, 12, 10, 15, 12, 13, 20, 14, 12, 6, 9], sum 142 - trying again.
Pass failed with list [9, 5, 13, 10, 15, 10, 13, 14, 7], sum 96 - trying again.
Pass failed with list [15, 12, 5, 19, 6, 5, 5, 17], sum 84 - trying again.
Pass failed with list [8, 5, 7, 11, 15, 16, 12, 18, 13], sum 105 - trying again.
Pass failed with list [15, 14, 10, 9, 8, 6, 10, 15, 18], sum 105 - trying again.
Pass failed with list [14, 17, 10, 13, 16, 8, 5, 6, 14], sum 103 - trying again.
Pass failed with list [10, 12, 19, 9, 5, 18, 12, 8, 9], sum 102 - trying again.
Pass failed with list [15, 10, 11, 19, 12, 12, 18, 15, 13, 8, 19, 11], sum 163 - trying again.
Pass failed with list [10, 20, 17, 11, 20, 11, 14, 13, 18, 5, 5], sum 144 - trying again.
Pass failed with list [20, 8, 11, 16, 18, 16, 15, 12, 9, 14, 15, 18, 13], sum 185 - trying again.
Pass failed with list [16, 7, 20, 11, 12, 16, 11, 9, 5, 13], sum 120 - trying again.
Pass failed with list [10, 12, 19, 9, 14, 15, 17, 19, 7, 11, 17, 17, 7], sum 174 - trying again.
Pass failed with list [5, 5, 6, 12, 10, 16, 10], sum 64 - trying again.
Pass failed with list [16, 18, 20, 14, 20, 19, 16, 7, 5, 12, 9, 11, 15], sum 182 - trying again.
Pass failed with list [14, 7, 13, 15, 16, 12, 20, 5, 5, 13], sum 120 - trying again.
Pass failed with list [17, 16, 9, 20, 13, 9, 9, 17, 19, 19, 7, 13, 18], sum 186 - trying again.
Pass failed with list [16, 11, 18, 17, 14, 16, 9, 10, 14, 19, 17, 6, 17], sum 184 - trying again.
Pass failed with list [12, 9, 9, 16, 10, 12, 18, 17, 16, 12, 18, 15], sum 164 - trying again.
[15, 14, 11, 14, 13, 13, 18, 19, 19, 13, 14, 6, 19, 16, 23]
>>> rng_list()
[13, 8, 15, 5, 17, 19, 14, 15, 17, 19, 20, 14, 17, 15, 19]
(Added empty lines in output for better readability.)