4 Random Numbers that equal the same Total

girlfriday

New Member
Joined
Jan 21, 2014
Messages
6
Could you tell me how to make a Excel spreadsheet where I could make 4 random numbers that when added together will equal the same total?

Such as the total is 21 so different 4 numbers would be 3558, 3648, 3747, 3828, etc

So I want to have a different total each time and the 4 digit numbers will be automatically generated for me.

Thank you ever so much!
 
My thinking is that the most random would be to generate 4 digits without constraints (the important part) then accept only those that sum to 21. Perhaps inefficient but would guarantee randomness.
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Generating four random numbers that total to a given value is equivalent to cutting a piece of string of the desired length in three places. The result should have a particular distribution of lengths. It can be done with formulas or a UDF; here's a formula method:

A​
B​
C​
D​
E​
1​
Target​
2​
21​
C2: Input
3​
Input​
Strings​
Output​
4​
0​
0.529​
11​
A4: Input
5​
1​
0.222​
5​
A5: Input
6​
0.529
0.115​
2​
A6 and down: =RAND()
7​
0.751
0.134​
3​
B4 and down: = SMALL($A$4:$A$8, ROWS(B$4:B4)+1) - SMALL($A$4:$A$8, ROWS(B$4:B4))
8​
0.866
C4 and down: =ROUND( B4 * (C$2 - SUM(C$3:C3)) / (1 - SUM(B$3:B3)), 0)
 
Upvote 0
Try This Solution:

A1 = The Number you are looking to total In this case 21

B1 =RANDBETWEEN(MAX($A$1-27,1),MIN($A$1,9))

C1 =IF($A$1-B1-18>=1,RANDBETWEEN(MAX($A$1-(18+B1),0),9),RANDBETWEEN(0,MIN($A$1-B1,9)))

D1 =RANDBETWEEN(MAX($A$1-B1-C1-9,0),MIN($A$1-B1-C1,9))

E1 =$A$1-SUM(B1:D1)

F1 =VALUE(B1&C1&D1&E1) This gives you what you are looking for.

This will work 2 - 36

Technically 1 but the first digit is always at least 1

HTH
 
Upvote 0
Try This Solution:

A1 = The Number you are looking to total In this case 21

B1 =RANDBETWEEN(MAX($A$1-27,1),MIN($A$1,9))

C1 =IF($A$1-B1-18>=1,RANDBETWEEN(MAX($A$1-(18+B1),0),9),RANDBETWEEN(0,MIN($A$1-B1,9)))

D1 =RANDBETWEEN(MAX($A$1-B1-C1-9,0),MIN($A$1-B1-C1,9))

E1 =$A$1-SUM(B1:D1)

F1 =VALUE(B1&C1&D1&E1) This gives you what you are looking for.

This will work 2 - 36

Technically 1 but the first digit is always at least 1

HTH

Actually, any jiggery-pokery like this will bias it I reckon. As suggesteded by a couple of other posters, the only prima faci guaranteed way to do it is to use the million monkeys approach and just keep looping in vba generating 4 random digits until you find a set that satisfies your criteria.
 
Last edited:
Upvote 0
Shg and my "cut a string 3 places randomly" approach works as randomly as the million monkey approach. The only adjustment would be rounding to get to integers. But, in both my VBA approach and Shg's spreadsheet approach, "adjust the last number up" occurs as often as "adjust the last number down" so it doesn't really matter.
 
Upvote 0
the only prima faci guaranteed way to do it is to use the million monkeys approach
That's a lot of monkeys, but not an algorithm. String cutting is rigorously correct.

Mike, your approach couples the four numbers -- it will have a very different distribution than string-cutting.
 
Last edited:
Upvote 0
Don't forget to save anything that looks like Shakespeare.
 
Upvote 0
Shg and my "cut a string 3 places randomly" approach works as randomly as the million monkey approach. The only adjustment would be rounding to get to integers. But, in both my VBA approach and Shg's spreadsheet approach, "adjust the last number up" occurs as often as "adjust the last number down" so it doesn't really matter.

Your way doesn't qualify Mike because it generates numbers between -1 and +21.
Mr shg has a similar problem with his solution. It returns numbers from 0 to 21 (with diminishing probability).

I assume that the numbers generated should be between 1 and 9. If zeros were allowed (which, although I don't know for sure I very much doubt), then it would have to allow a leading zero or have more jiggery pokery to manage the first digit as a special case.

My argument as stated above is that the algorithm of simply trying all cases (random numbers between 1 and 9 inclusive) and discarding the sets that don't add up to 21 is correct prima faci. By that I mean "on the face of it" or "on the surface". In other words without having to offer a rigorous proof to support its correctness.

Any additions to your algorithms to bring them back to the allowable space will take it further from the prima faci correct mega-monkey algorithm and this would require more work to prove that they are correct.

Your solution and Mr shg's, while very clever and creative, are not only not correct prima faci, they are clearly incorrect.

Randomness is surprisingly delicate and random systems have to be treated very carefully and conservatively in order not to leave finger prints. Thats is my basic point. And anyway, I did a little test and my 1.36 million monkeys took 25 seconds to generate and document 100,000 acceptible solutions, an average of 250 micro seconds and with a 99.99% chance of doing it in under 22 milliseconds, so there is no perceptible delay with the monkey approach and therefore no reason not to use it.

Here is the code to do it

Code:
Sub Rand4()
'Select as many output rows as you want in a column and the results will be output to the right.
'For a single solution, just select one cell.
Const limit As Long = 21
Dim r(1 To 4) As Long, rSum As Long, i As Long, c As Range, attemptsCount As Long
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
For Each c In Selection
    attemptsCount = 0
    Do
        rSum = 0
        For i = 1 To 4
            r(i) = Round(Rnd() * 9 + 0.5, 0)
            rSum = rSum + r(i)
        Next i
        attemptsCount = attemptsCount + 1
    Loop Until rSum = limit
    c.Value2 = attemptsCount
    For i = 1 To 4
        c.Offset(0, i).Value2 = r(i)
    Next i
Next c
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0
That's a lot of monkeys, but not an algorithm. String cutting is rigorously correct.

Sorry, I didn't have time for a full reply yesterday...

Actually your string cutting is not correct rigorously or otherwise, simply because you generate solutions that have members greater than 9.
I was interested in this aproach and I wanted to simulate it and check the distribution against the cannonical monkeys, because I thought it would be interesting. The only problem is you would need to do some heavy manipulation on the cuts and couple them in fact, in order to keep them spaced less than 10. Once I realised that I gave up. I coded it up in its raw form and there is already considerable overhead to manage the transformation from cuts to output: sort in place, running totals; so I figured it was getting too complicated and bid a hasty and undignified retreat. Maybe you have an ingenious way to do it?
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

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