generating a set of random numbers to total a set value

yaldan

New Member
Joined
Jan 15, 2007
Messages
1
Is it possible to generate a set of random numbers between say 50 - 150 whereby the sum of those random numbers totals a value in the spreadsheet which is determined by two other values in the spreadsheet?

Eg: value 1 = 10, value 2 = 300. The difference = 290. Now generate 6 or so random numbers between 50 - 150 that come to a total of 290.

If so could you helpme with formula or script.

Regards

Yaldan
 
Sorry I could not edit my post but I could not explain it well.I am retyping again as it seems a little bit silly at the moment
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
With only 2 numbers, each of which has a max value of 25, that must sum up to 50, there is only 1 possible combination: 25 & 25. If you punch F9 enough, you'll get that combination.

I had thought of adding a formula that says something like "Solution is not valid, press F9".

I also thought of changing the formulas so that they would always generate a valid solution. But the problem there is that a) the formulas would be much more complicated, and b) the further down the list you go, the less "random" the numbers would get. They'd start trending high or low, which may well invalidate the randomness of the results.

Final option would be to write a VBA function that generates the list for you. It would essentially do the same thing as these functions, but automatically recalculate in case of an invalid result.

Let me know if any of those options appeal.
 
Upvote 0
I need number 1 and number 2 to add up in such a way that they target the total (E1)
The "total"(E1) can be 100 maximum at any given time(I will decide the total) and number 1 can be 25 and number 2 can be 25 maximum). In this example the total(E1) is 85 so, for example A2 can be 15 and B2 can be 20. (since I am going to change the total I would really be glad if I could specify a minimum for the number1 and number 2 too)
I hope I could explain myself this time.
Screen_Shot_2016_02_09_at_20_44_47.png

Thanks a lot
 
Upvote 0
OK, I created some formulas that will always generate a valid set of numbers, with no repeated F9. But as I warned you, the formulas are more complicated.

Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
21005257
3
420
521
622
718
87
97
105100

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
A4=RANDBETWEEN(MAX($B$2,$A$2-(($D$2-ROWS($A$4:$A4))*$C$2)),MIN($C$2,$A$2-(($D$2-ROWS($A$4:$A4))*$B$2)))
A5=IF(ROW()=$D$2+3,$A$2-SUM($A$4:$A4),IF(ROW()>$D$2+3,"",RANDBETWEEN(MAX($B$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$C$2)),MIN($C$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$B$2)))))
B4=IF(OR($C$2*D$2<$A$2,$B$2*$D$2>$A$2),"Can't be done!","")
B10=SUM(A4:A20)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



Here's your 2-number example:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2505252
3
425
525
6
7
8
9
1050

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



From your example in post #13:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2355252
3
418
517
6
7
8
9
1035

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



Since your numbers 3 and 4 are fixed, subtract their total (50) from the overall total (85) to get the value (35) that numbers 1 and 2 must sum to.
 
Upvote 0
OK, I created some formulas that will always generate a valid set of numbers, with no repeated F9. But as I warned you, the formulas are more complicated.

Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
21005257
3
420
521
622
718
87
97
105100

<tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
A4=RANDBETWEEN(MAX($B$2,$A$2-(($D$2-ROWS($A$4:$A4))*$C$2)),MIN($C$2,$A$2-(($D$2-ROWS($A$4:$A4))*$B$2)))
A5=IF(ROW()=$D$2+3,$A$2-SUM($A$4:$A4),IF(ROW()>$D$2+3,"",RANDBETWEEN(MAX($B$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$C$2)),MIN($C$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$B$2)))))
B4=IF(OR($C$2*D$2<$A$2,$B$2*$D$2>$A$2),"Can't be done!","")
B10=SUM(A4:A20)

<tbody>
</tbody>

<tbody>
</tbody>



Here's your 2-number example:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2505252
3
425
525
6
7
8
9
1050

<tbody>
</tbody>
Sheet1



From your example in post #13:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2355252
3
418
517
6
7
8
9
1035

<tbody>
</tbody>
Sheet1



Since your numbers 3 and 4 are fixed, subtract their total (50) from the overall total (85) to get the value (35) that numbers 1 and 2 must sum to.

Thanks a lot. It really really helped me..
 
Upvote 0
Respected Sir,
i have read your post on generate random numbers for a specific total.
I'm a high school teacher required same thing in a columns.... i have tried but not success..
will you help me for this.. i need random numbers in the columns..
i have attached my screen shot....
i want to put total marks in a total coulmn... and want it will convert in five random number exactly as i put total in a column...
Sir... I'm very thankful to you.... and if possible plzzz give your algorithm too.... so i can understand as well help my students for this...
Thanksss again

Mark1Mark2Mark3Mark4Mark5Total
56410934
9859839
101086943
2178523

<colgroup><col style="width:48pt" span="6" width="64"> </colgroup><tbody>
</tbody>
 
Upvote 0
OK, I created some formulas that will always generate a valid set of numbers, with no repeated F9. But as I warned you, the formulas are more complicated.

Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
21005257
3
420
521
622
718
87
97
105100

<tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
A4=RANDBETWEEN(MAX($B$2,$A$2-(($D$2-ROWS($A$4:$A4))*$C$2)),MIN($C$2,$A$2-(($D$2-ROWS($A$4:$A4))*$B$2)))
A5=IF(ROW()=$D$2+3,$A$2-SUM($A$4:$A4),IF(ROW()>$D$2+3,"",RANDBETWEEN(MAX($B$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$C$2)),MIN($C$2,$A$2-(SUM($A$4:$A4)+($D$2-ROWS($A$4:$A5))*$B$2)))))
B4=IF(OR($C$2*D$2<$A$2,$B$2*$D$2>$A$2),"Can't be done!","")
B10=SUM(A4:A20)

<tbody>
</tbody>

<tbody>
</tbody>



Here's your 2-number example:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2505252
3
425
525
6
7
8
9
1050

<tbody>
</tbody>
Sheet1



From your example in post #13:
Excel 2010
ABCD
1TargetMinMaxNumber of random numbers
2355252
3
418
517
6
7
8
9
1035

<tbody>
</tbody>
Sheet1



Since your numbers 3 and 4 are fixed, subtract their total (50) from the overall total (85) to get the value (35) that numbers 1 and 2 must sum to.

Respected Sir,
i have read your post on generate random numbers for a specific total.
I'm a high school teacher required same thing in a columns.... i have tried but not success..
will you help me for this.. i need random numbers in the columns..
i have attached my screen shot....
i want to put total marks in a total coulmn... and want it will convert in five random number exactly as i put total in a column...
Sir... I'm very thankful to you.... and if possible plzzz give your algorithm too.... so i can understand as well help my students for this...
Thanksss again

Mark1Mark2Mark3Mark4Mark5Total
56410934
9859839
101086943
2178523

<tbody>
</tbody>
And I'm sorry to Admin... i'm new to this forum so i don't know how to del my first post which is without quote of this important thing... please forgive me....
 
Upvote 0
Here is a version that works horizontally, with 5 values as in your example:

Excel 2010
ABCDEFGHIJKLM
1MinMaxNumber of random numbers
25255Mark1Mark2Mark3Mark4Mark5Total
311665634
417556639
513679843
6#NUM!#NUM!#NUM!#NUM!#NUM!23
7
8

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet15

Worksheet Formulas
CellFormula
G3=RANDBETWEEN(MAX($B$2,L3-(($D$2-COLUMNS($G3:G3))*$C$2)),MIN($C$2,L3-(($D$2-COLUMNS($G3:G3))*$B$2)))
H3=IF(COLUMN()=$D$2+COLUMN($G$3)-1,$L3-SUM($G3:G3),IF(COLUMN()>$D$2+COLUMN($G$3)-1,"",RANDBETWEEN(MAX($B$2,$L3-(SUM($G3:G3)+($D$2-COLUMNS($G3:H3))*$C$2)),MIN($C$2,$L3-(SUM($G3:G3)+($D$2-COLUMNS($G3:H3))*$B$2)))))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



Paste the G3 formula in G3. Then paste the H3 formula in H3, then copy H3 across to K3. Then copy G3:K3 and paste them down the column as far as you need. The #NUM! values in row 6 indicate that there is no possible solution.

As far as the algorithm, here's a quick rundown:

If you are selecting 5 numbers that must add up to a certain value, and you have already picked 4 of them, then the fifth number MUST be the target sum, minus the sum of the other four. Part of the H3 formula does just that, it looks to see if it's in the right column, and if so, does the subtraction.

Now for the previous 4 numbers, we have defined low and high values, B2 and C2. However, the more numbers we pick, the more constraints we have on the maximum and minimum values. For example, look at row 3. The first number picked was 11. Now we want to pick the second number. The total is 34, minus 11, leaves us with 23. Now let's assume that the 3rd, 4th and 5th numbers are all as low as possible, which is 5. 23 - 5 - 5 - 5 = 8. So although the theoretical maximum number for the second number is 25, at this point in the selection process, the actual maximum is 8. We do this using MIN(25, 8). Using a similar process assuming that the 3rd, 4th and 5th numbers are as high as possible (25), we find that the minimum value is MAX(5, -8), or 5. So we do a RANDBETWEEN(5, 8).

And we just repeat. The G3 formula calculates a RANDBETWEEN based on the number of values, the max and min values, and the target number. H3 to J3 perform the same thing, only they include the sum of the previously selected numbers so far. And the K3 formula is just the target minus the sum of the numbers chosen.

In principle, not too hard, but it is kind of tricky to incorporate into formulas. Take a look and let me know if you can't figure out the pieces.

Out of curiosity, where are you located, and what subject are you teaching?

Good luck!
 
Upvote 0
Here is a version that works horizontally, with 5 values as in your example:

Excel 2010
ABCDEFGHIJKLM
1MinMaxNumber of random numbers
25255Mark1Mark2Mark3Mark4Mark5Total
311665634
417556639
513679843
6#NUM!#NUM!#NUM!#NUM!#NUM!23
7
8

<tbody>
</tbody>
Sheet15

Worksheet Formulas
CellFormula
G3=RANDBETWEEN(MAX($B$2,L3-(($D$2-COLUMNS($G3:G3))*$C$2)),MIN($C$2,L3-(($D$2-COLUMNS($G3:G3))*$B$2)))
H3=IF(COLUMN()=$D$2+COLUMN($G$3)-1,$L3-SUM($G3:G3),IF(COLUMN()>$D$2+COLUMN($G$3)-1,"",RANDBETWEEN(MAX($B$2,$L3-(SUM($G3:G3)+($D$2-COLUMNS($G3:H3))*$C$2)),MIN($C$2,$L3-(SUM($G3:G3)+($D$2-COLUMNS($G3:H3))*$B$2)))))

<tbody>
</tbody>

<tbody>
</tbody>



Paste the G3 formula in G3. Then paste the H3 formula in H3, then copy H3 across to K3. Then copy G3:K3 and paste them down the column as far as you need. The #NUM! values in row 6 indicate that there is no possible solution.

As far as the algorithm, here's a quick rundown:

If you are selecting 5 numbers that must add up to a certain value, and you have already picked 4 of them, then the fifth number MUST be the target sum, minus the sum of the other four. Part of the H3 formula does just that, it looks to see if it's in the right column, and if so, does the subtraction.

Now for the previous 4 numbers, we have defined low and high values, B2 and C2. However, the more numbers we pick, the more constraints we have on the maximum and minimum values. For example, look at row 3. The first number picked was 11. Now we want to pick the second number. The total is 34, minus 11, leaves us with 23. Now let's assume that the 3rd, 4th and 5th numbers are all as low as possible, which is 5. 23 - 5 - 5 - 5 = 8. So although the theoretical maximum number for the second number is 25, at this point in the selection process, the actual maximum is 8. We do this using MIN(25, 8). Using a similar process assuming that the 3rd, 4th and 5th numbers are as high as possible (25), we find that the minimum value is MAX(5, -8), or 5. So we do a RANDBETWEEN(5, 8).

And we just repeat. The G3 formula calculates a RANDBETWEEN based on the number of values, the max and min values, and the target number. H3 to J3 perform the same thing, only they include the sum of the previously selected numbers so far. And the K3 formula is just the target minus the sum of the numbers chosen.

In principle, not too hard, but it is kind of tricky to incorporate into formulas. Take a look and let me know if you can't figure out the pieces.

Out of curiosity, where are you located, and what subject are you teaching?

Good luck!


Respected Mr. Eric Sir,
Thanksssss sooo much for your kind help.... Thankssss sooo much...
I'm a high school teacher teaching maths, sci and computer science.... my school located in Ahmedabad, India...
and thanks a lot for algorithm too... i'll quite helpful to me and my students..... I have personel message you in this forum but i think it is not reached to you..
(because it allows only on post for me.... i thik so)..
do you have FB? i really like to add you there..
thankss again
and nice meet you sir
 
Upvote 0
I'm glad I could help. I'm one of the few people left in the world who's not on Facebook. If you have a question, send me a message here and I'll get back to you.
 
Upvote 0

Forum statistics

Threads
1,215,205
Messages
6,123,634
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top