This seems like a simple problem but I don't know of a way to efficiently solve it:
Suppose I want to generate random samples of 100 numbers, which satisfy the constraint that the sum is less than a certain number:
import random
import numpy as np
#numbers to sample from, could be a more complicated distribution
list_of_numbers = np.random.rand(1000) * 50
valid_samples = list()
while(len(valid_samples) < required_length):
sample = [random.choice(list_of_numbers) for _ in range(100)]
if (sum(sample) < 1000):
valid_samples.append(sample)
The problem is that I end up generating a vast majority of samples that just get thrown away. However, it doesn't seem clear how to implement the constraint on the list of numbers, since there would be mutual dependencies involved. I can't throw out any of the original list, since none of the numbers exceeds the threshold for the sample (1000). Right now, it takes a very long time to obtain the sample, and I would like to find a faster way to do so.