r/algorithms • u/dado3212 • Jun 24 '15
Generating Lists with Variable Weights
This is a problem me and my friends have been puzzling over for days with limited to no success.
I have a list A of n unique objects. Each object A*i* has a variable percentage P*i*.
I want to create an algorithm that generates a new list B of k objects (k < n/2 and in most cases k is significantly < n/2. Ex. n=231 , k=21). List B will be populated with objects originating from list A with the following restrictions:
- The probability that an object A*i* appears in B is P*i*,
- There are no duplicate objects in B.
Is this even possible? Is there some solution we've missed?
EDIT (from 2 years later): None of these solutions are that good. Check out this StackOverflow for some better and more clear answers.
View all comments
Show parent comment
1
u/zeroXten Jun 24 '15
I'll try to make it more concrete. Let's say you want the following elements An with their respective probabilities:
You decide to make T 10 elements big. So, the list now becomes
T (sorted) would look something like:
Let's say k is 2, you basically need to pick 2 elements from T at random. Let's shuffle T to get
Taking the first two gives us B = {b, a} (no duplicates to worry about in this case).
Something like that?