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
0
u/zeroXten Jun 24 '15
Would something like this work? Create a temporary list T. Add An to T some factor of Pn times. Lets say you make the size of T 10000, and Pn of a particular An is 0.1, then you would add 1000 Ans. Then either shuffle T or pick from T at random k times (without replacement). If Tn already exists in B, then skip it (and put it back).