Randomly Selecting with Average
The goal of this project was to “randomly” select numbers from a predefined set, with replacement, in a way that the mean of the selected numbers would equal (or come close to) a specified number. For example, and the original motivation, was to select 100 numbers from the set: so that the mean of the selected numbers was
Said a different way, given and , find such that:
Now obviously, this isn’t something that can be solved deterministically, and there might be many different ways of selecting our . For example, consider , , and . We would be right to select 20 “0.5”s, or 10 “0”s and 10 “1.0”s. Both methods would create a mean exactly equal to 0.5. Therefore, it would be nice to have some sort of parameter that determined the “shape” of our selection, whether the selection was all from the extremes, or tightly grouped around the mean.
In order to solve both these issues, I decided to randomly select numbers from a probability distribution with finite support on and round to the nearest .
Consider the beta distribution
where and . The beta distribution is a nice choice for this problem for two reasons. First is because of its finite support on the interval . Second, its mean is very easy to calculate.
This means we can intelligently select and (or select one and fix the other using the above), select random numbers, round to the nearest and the average should be pretty close to . This turned out to be good enough for my purposes, and the program to do this is written below.