Hi
I want to generate two random integers that fall within a certain range
("min" to "max", e.g. 0 to 10) AND that sum to equal no more than a given
value ("maxsum", e.g. 19)
The first integer is easy (using a Analysis Toolpak add-in)
A1=RANDBETWEEN(min, max)
But that second integer has me stuck. I started with A2=RANDBETWEEN(min,
maxsum-A1) but then a lot of the results were larger than "max." Oops. Any
ideas on what else I need to put in there?
Thanks,
Karin
You have to be careful when you use the word random.
Which are your requirements on randomness?
One interpretation of "random" is that each valid result should have
the same probability, 1/N where N is the number of valid results.
The proposed formulas, so far, does not fulfil this requirement.
As soon as you restrict the second number based on the value of the
first number, you have lost some kind of randomness.
If you don't have this requirement there are some very simple
"solutions" to your problem that you probably don't like, e.g.
A1= min,
A2 = max
or
A1 = RANDBETWEEN(min, maxsum/2),
A2 = RANDBETWEEN(min, maxsum/2)
If you have the requirement that two numbers, x and y, should each
have an integer value from min to max, but their sum should never
exceed maxsum, there is a reduction of the number of valid results N,
from N = (max-min+1)^2 to
N = (max-min+1)^2- (maxsum-max-min)*(maxsum-max-min+1)/2
If min = 0, max = 10 and maxsum = 15
then N = (10-0+1)^2 - (15-10-0)*(15-10-0+1)/2 = 106
(compared to 121 if there was no maxsum restriction)
If you do have the requirement that each of these 106 result should
have the same probability, then one way to generate the numbers would
be to list all 106 valid combinations of (x,y) in the range A1:B106.
(this could be done manually or with formulas)
Then you use the following array formula in cell C1
1
=INDEX(Á1:B106;RANDBETWEEN(1;106);COLUMN(A:B))
Note: This is an array formula that has to be confirmed by
CTRL+SHIFT+ENTER rather than just ENTER.
Hope this helps / Lars-Åke