0

I'm trying to work out how to choose an item at random, based on a couple of weights. Let me explain.

  • I have a person object which has an age group assigned, young, middle or old. This age group is a known variable.
  • I have 3 groups, each with their own chance of being selected, but also weighted towards an age group.

For example:

  • Group 1 has a 50% chance of being chosen, but more if they are young.
  • Group 2 has a 30% chance of being chosen by any age group.
  • Group 3 has a 20% chance of being chosen, but more if they are middle aged.

I'm not looking for a code hand out, but more theory to push me towards the correct answer. I know how to pick a group based on it's percentage alone, but I'm not quite sure how to weight it based on age group..

Thanks in advance. :)

Philip Lewis
  • 57
  • 2
  • 9
  • An iterative solution would be to first sum all the weights, and then generate a random number in that range. Then for each element with a weight, if the random number is less than or equal to the weight, you found the element, otherwise subtract the element weight from the random number and move to the next. So for your example, generate a random number from 0 to 100. Let's say you generated 75. 75 is not less than or equal to 50, so subtract 50 getting 25. 25 is less than or equal to 30, so you found Group 2. – Lasse V. Karlsen Jul 27 '20 at 10:09
  • 1
    The problem is the "but more if they are young". With how vague this is described, all I can say is "and then you adjust for this". – Lasse V. Karlsen Jul 27 '20 at 10:12
  • That just sounds like choosing based on percentages only, not adjusting for age group? The 'more if they are young' just means if the person is in the young group, then you'll be more likely to select group 1 over the other groups. – Philip Lewis Jul 27 '20 at 10:13
  • put the person (reference) into a pool (array or whatever, but thats from where you pick) N times (where N = weight). so group 1 dude is put in 5 times, group 3 dude is put in 3 times etc ..get a random number between 1 and poolsize -> that index is your selected person. its weighted – Michael Schönbauer Jul 27 '20 at 10:13
  • @PhilipLewis And as I stated above, that's too vague to answer. All I can say is "well, then you need to adjust for that". The question is, **how** do you want to adjust for that? If a person is young, how likely is it supposed to be then that group 1 is chosen? You likely want to just adjust those percentages based on their age groups, but you need to figure out yourself what that adjustment is. Perhaps you need a lookup table that has 3 percentages per age group? For instance, `young = 60, 30, 10`, `middle = 40, 30, 30`, and `rest = 50, 30, 20` ? – Lasse V. Karlsen Jul 27 '20 at 10:19
  • Thanks for your help everyone. I just ended up doubling the chance of picking a group if the person was in the 'preferred' age group. Inelegant, but effective. – Philip Lewis Jul 27 '20 at 12:50
  • If you found a solution, post it as an answer. – Peter O. Jul 27 '20 at 14:09

1 Answers1

0

Philip Lewis's comment:

Thanks for your help everyone. I just ended up doubling the chance of picking a group if the person was in the 'preferred' age group. Inelegant, but effective.

Peter O.
  • 32,158
  • 14
  • 82
  • 96