formula for pyramid distribution? For Google Sheets

smking204

New Member
Joined
Jan 28, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I've got a doozy for y'all. I want to create a formula that will return a random integer according to a pyramid distribution.

For example, A1 might serve as my input, which is itself a randomized integer, let's call it 24 for now. In B1, then, I want my pyramid formula, which will return a new integer that is + or - 50% of the input, but weighted toward the middle (12 and 36 would each have one chance of appearing, 13 and 35 would each have two chances, 14 and 34 would each have 3, and so on).

The formula needs to be 1) dependent on the variable input, and 2) able to be retooled for a different percentage

WAY too much math for me, but I hope to get some responses to dissect and get a little better at this sort of thing. Thanks a ton!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I am a little unclear as to the final result that you want. Are you looking for a single formula that returns a single value picked according to your stated requirement? Of are you looking for something else? If so, what?
 
Upvote 0
I am a little unclear as to the final result that you want. Are you looking for a single formula that returns a single value picked according to your stated requirement? Of are you looking for something else? If so, what?
thanks, yes. I'm looking for a single integer.
 
Upvote 0
I spent wayyyy too much time playing around with this, but I came up with a function:

Book1 (version 1).xlsb
ABCDE
10MidpointRangeRandom #
202450%21
30
406
50
60
70
80
90
100
110
1217.................
1329.............................
1433.................................
1538......................................
1673.........................................................................
1760............................................................
1872........................................................................
1999...................................................................................................
20111...............................................................................................................
21117.....................................................................................................................
22143...............................................................................................................................................
23151.......................................................................................................................................................
24155...........................................................................................................................................................
25157.............................................................................................................................................................
26133.....................................................................................................................................
27114..................................................................................................................
28110..............................................................................................................
2980................................................................................
3090..........................................................................................
3171.......................................................................
3250..................................................
3335...................................
3425.........................
3525.........................
3612............
370 
380 
390 
400 
End
Cell Formulas
RangeFormula
E2E2=LET(r,INT(B2*C2)+1,s,SEQUENCE(,r*2-1,0),si,s*(s+1)/2,sd,IFERROR(INDEX(si,EXP(LN(s-r+1)))*2,0),fs,si-sd+1,v,MATCH(RANDBETWEEN(1,MAX(fs)),fs),B2-r+v)
E4E4=INT(NORM.INV(RAND(),B2,B2*C2/2))
B12:B40B12=REPT(".",A12)


The E2 formula does what you want. It delivers a random number within the midpoint and range selected, weighted by your pyramid design. I ran a macro that calculated it 2000 times and plotted the results in rows 12 to 36, the row corresponding to the result numbers. Then a simple plot line to show how often each number was selected. As you can see, the result is very close to being a triangle shape.

However, if you're a bit more flexible and you can use a normal distribution, Excel has a built-in formula that does that. See the E4 formula. When I plotted 2000 results the same as I did for the E2 formula, the resulting plot was nearly indistinguishable (other than a few numbers trailing off the ends).

Hope you find this useful!
 
Upvote 0
I spent wayyyy too much time playing around with this, but I came up with a function:

Book1 (version 1).xlsb
ABCDE
10MidpointRangeRandom #
202450%21
30
406
50
60
70
80
90
100
110
1217.................
1329.............................
1433.................................
1538......................................
1673.........................................................................
1760............................................................
1872........................................................................
1999...................................................................................................
20111...............................................................................................................
21117.....................................................................................................................
22143...............................................................................................................................................
23151.......................................................................................................................................................
24155...........................................................................................................................................................
25157.............................................................................................................................................................
26133.....................................................................................................................................
27114..................................................................................................................
28110..............................................................................................................
2980................................................................................
3090..........................................................................................
3171.......................................................................
3250..................................................
3335...................................
3425.........................
3525.........................
3612............
370 
380 
390 
400 
End
Cell Formulas
RangeFormula
E2E2=LET(r,INT(B2*C2)+1,s,SEQUENCE(,r*2-1,0),si,s*(s+1)/2,sd,IFERROR(INDEX(si,EXP(LN(s-r+1)))*2,0),fs,si-sd+1,v,MATCH(RANDBETWEEN(1,MAX(fs)),fs),B2-r+v)
E4E4=INT(NORM.INV(RAND(),B2,B2*C2/2))
B12:B40B12=REPT(".",A12)


The E2 formula does what you want. It delivers a random number within the midpoint and range selected, weighted by your pyramid design. I ran a macro that calculated it 2000 times and plotted the results in rows 12 to 36, the row corresponding to the result numbers. Then a simple plot line to show how often each number was selected. As you can see, the result is very close to being a triangle shape.

However, if you're a bit more flexible and you can use a normal distribution, Excel has a built-in formula that does that. See the E4 formula. When I plotted 2000 results the same as I did for the E2 formula, the resulting plot was nearly indistinguishable (other than a few numbers trailing off the ends).

Hope you find this useful!
Oh, wow, thanks! Plenty for me to dig into here. I should also clarify that I need it to work in Google Sheets. Apparently =LET doesn't exist there yet, which is a shame. I'll try to note this in my original post. I'll see what work-arounds I can find in the meantime.
 
Upvote 0
We have a special forum called "General Discussion and Other Applications" where questions about other spreadsheet programs should be placed. And definitely mention the name of the program you're using. It would have saved me a lot of time. I really don't know much about Google Sheets, so I can't really tell you how to adapt the formula for GS. In theory, any LET function can be rewritten without LET, but it would be probably 5 or 6 times longer. And I'm not going to do that for fear that I'll get caught in another difference between the 2 programs.

My best advice would probably be to use the NORM.INV function (if it exists in GS), and add a section to see if the returned value exceeds the end points (which would raise the probability of hitting an end point, but maybe not by enough to worry about).
 
Upvote 0
I spent wayyyy too much time playing around with this, but I came up with a function:

Book1 (version 1).xlsb
ABCDE
10MidpointRangeRandom #
202450%21
30
406
50
60
70
80
90
100
110
1217.................
1329.............................
1433.................................
1538......................................
1673.........................................................................
1760............................................................
1872........................................................................
1999...................................................................................................
20111...............................................................................................................
21117.....................................................................................................................
22143...............................................................................................................................................
23151.......................................................................................................................................................
24155...........................................................................................................................................................
25157.............................................................................................................................................................
26133.....................................................................................................................................
27114..................................................................................................................
28110..............................................................................................................
2980................................................................................
3090..........................................................................................
3171.......................................................................
3250..................................................
3335...................................
3425.........................
3525.........................
3612............
370 
380 
390 
400 
End
Cell Formulas
RangeFormula
E2E2=LET(r,INT(B2*C2)+1,s,SEQUENCE(,r*2-1,0),si,s*(s+1)/2,sd,IFERROR(INDEX(si,EXP(LN(s-r+1)))*2,0),fs,si-sd+1,v,MATCH(RANDBETWEEN(1,MAX(fs)),fs),B2-r+v)
E4E4=INT(NORM.INV(RAND(),B2,B2*C2/2))
B12:B40B12=REPT(".",A12)


The E2 formula does what you want. It delivers a random number within the midpoint and range selected, weighted by your pyramid design. I ran a macro that calculated it 2000 times and plotted the results in rows 12 to 36, the row corresponding to the result numbers. Then a simple plot line to show how often each number was selected. As you can see, the result is very close to being a triangle shape.

However, if you're a bit more flexible and you can use a normal distribution, Excel has a built-in formula that does that. See the E4 formula. When I plotted 2000 results the same as I did for the E2 formula, the resulting plot was nearly indistinguishable (other than a few numbers trailing off the ends).

Hope you find this useful!
also, I'm looking into your E4 formula and that "normal distribution" concept. Seems like "Normal Distribution" is a perfectly good way to go with this, but I do need to limit the results to +/- 50%

EDIT: I just saw the above two replies, sorry for the confusion and thanks for the help!
 
Last edited:
Upvote 0
I tweaked the normal distribution formula so that it limits the results to +/- 50%. It works, but the endpoints will be a bit higher than expected (see the graph below). I experimented with changing the size of the standard deviation. I originally used a divisor of 2 which made the endpoints significantly higher. I used a divisor of 3, which made the endpoints about what you'd want, but it made the rest of the values drop off much quicker too. I think a divisor of 2.5 is about the best compromise.

Book1 (version 1).xlsb
ABCDE
10MidpointRangeRandom #
202450%25
30
40
50
60
70
80
90
100
110
1225.........................
1311...........
1429.............................
1539.......................................
1650..................................................
1756........................................................
1889.........................................................................................
19111...............................................................................................................
20115...................................................................................................................
21140............................................................................................................................................
22154..........................................................................................................................................................
23200........................................................................................................................................................................................................
24168........................................................................................................................................................................
25153.........................................................................................................................................................
26132....................................................................................................................................
27116....................................................................................................................
28109.............................................................................................................
2978..............................................................................
3073.........................................................................
3139.......................................
3240........................................
3325.........................
3424........................
359.........
3615...............
370 
End
Cell Formulas
RangeFormula
E2E2=MEDIAN(INT(NORM.INV(RAND(),B2,B2*C2/2.5)),INT(B2-B2*C2),ROUNDUP(B2+B2*C2,0))
B12:B37B12=REPT(".",A12)




As far as I know, all these functions exist in Google Sheets, although with slightly different names (NORM.INV --> NORMINV). I hope it works for you, but if not, you'll have to find someone else to step in. Good luck!
 
Upvote 0

Forum statistics

Threads
1,215,388
Messages
6,124,652
Members
449,177
Latest member
Sousanna Aristiadou

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