while difference != 0:
    if difference < 0:
        for idx in np.random.choice(list(range(len(new_arr))), abs(difference)):
            if new_arr[idx] != range_list[1]:
                new_arr[idx] += 1
if difference > 0:
    for idx in np.random.choice(list(range(len(new_arr))), abs(difference)):
        if new_arr[idx] != 0 and new_arr[idx] != range_list[0]:
            new_arr[idx] -= 1
difference = sum(new_arr) - sum_to
return new_arr
new_arr = sampler(2872, 30000, [5, 15])
print('Generated random array is :')
print(new_arr)
print('Length of array:', len(new_arr))
print('Max of array: ', max(new_arr))
print('min of array: ', min(new_arr))
print('and it sums up to %d' % sum(new_arr))
Generate N positive integers within a range adding up to a total in python