Imagine your dilemma as picking marbles from a container without putting them back in. There are 120 marbles, with 12 of each among 10 different colors. Reserve 8 marbles of each color. Toss the remaining 40 marbles into a bag and draw 20 out randomly, combining them with the set-aside 80 marbles. You now possess 100 marbles, each color varying between 8 to 12 marbles. The distribution of numbers will not be evenly spread out. Look up "sampling without replacement" for the distribution formula.
For your particular scenario, input the following into A1:A10
:
=FREQUENCY(INT((ROUND(MOD(SMALL(RANDBETWEEN(-1e12,SIGN(ROW(INDIRECT("1:40")))*1e12)+ROW(INDIRECT("1:40"))/100,ROW(INDIRECT("1:20"))),1),2)*100-1)/4)+1,ROW(INDIRECT("1:10")))+8
Finalize with Ctrl
+Shift
+Enter
.
To make it more general, arrange your spreadsheet like this:
B1
: the total count of random numbers needed (e.g., 10)
B2
: the lowest value (e.g., 8)
B3
: the highest value (e.g., 12)
B4
: the desired sum (e.g., 100)
B5
: =(B3-B2)*B1
(count of available slots)
B6
: =CEILING.MATH(LOG10((B3-B2)*B1))
B7
: =10^B6
B8
: =B4-B1*B2
(slots to fill)
The formula for general use is:
=FREQUENCY(INT((ROUND(MOD(SMALL(RANDBETWEEN(-1e12,SIGN(ROW(INDIRECT("1:"&B5)))*1e12)+ROW(INDIRECT("1:"&B5))/B7,ROW(INDIRECT("1:"&B8))),1),B6)*B7-1)/(B3-B2))+1,ROW(INDIRECT("1:"&B1)))+B2
Recalculate the sheet to get a fresh set of numbers (F9
in Excel).