Positive random number generator from Normal distribution

susuru

New Member
Joined
Oct 22, 2009
Messages
9
Hi
<o:p> </o:p>
I would like to get random number generator for normal distribution and I’m using:
<o:p> </o:p>
=NORMINV(RAND(),$B$1,$B$2)
<o:p> </o:p>
Where B1 is mean (average) and B2 is SD.
<o:p> </o:p>
This command works, but in the random generating numbers, I’m getting positive and negative numbers (ex. -23,34).:(
<o:p> </o:p>
Please, how to change command so I can generate positive only numbers?


Thanks.
 
The problem with normal distribution you are looking for, is that you are loking for some other distribution, not normal.

So, tou must decide what distribution you need to generate your numbers and, after this decision, start trying generate them.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Quite so.

Yet, my main problem now is based around generating random positive numbers 0 - 300 with pre given SD and mean. No need to be from normal distribution.


Someone got formula for that?

Thanks
 
Upvote 0
Re: Positive random number generator from Normal distribution

susuru:

There are expensive statistical software packages available.
In many cases Excel will handle the job.

The solution may depend on what you are trying to accomplish and why.
As wsjackman noted the normal distribution may not be best for simulated time data.
The normal distribution can be used for simulations of data.
The normal distribution can be used if both side of the plot are cut off.

In Column B is a simulated PDF (Probability Distribution Function) for the Normal Distribution, with the tailing ends of the curve cut off on both sides, normalized to 1.
Note this is not a series of integers.

IN A2: =SUM(A4:A23)
IN B2: =SUM(B4:B23) {Normalized distribution = 1}
IN A4 (copied down):
=NORMDIST(ROW()-3,D$1,B$1,0)
IN B4 (copied down):
=A4/A$2

IN A25: =STDEV(A4:A23)
IN A26: =STDEVP(A4:A23)

The sum of the PDF is used in VLOOKUP:
D4 = 0
IN D5 (copied down):
=SUM(B$4:B4)

For integers 1 to 20 (instead of 300), std = 4 (Cell B1), Center = 10.5 (Cell D1)



Next, the normalized PDF can be used to generate a series of integers.
For the data below note that column B can be plotted on the x axis and Column C on the y axis.
Below is the histogram showing that in Column B there were 3 'Integer 8' and 0 'Integer 9' and 2 'Integer 10'.
Start with 20 integers that can be randomly selected according to the PDF from 1 to 20.
Starting in Cell B31, select B31 to B50 and enter, with CTRL+SHIFT+ENTER:
=FREQUENCY(C31:C50,A31:A50)
This will appear in Cells B31 to B50 as:
{=FREQUENCY(C31:C50,A31:A50)}
In Cell C31 (copied down to C50):
=VLOOKUP(RAND(),D$4:E$23,2)
Code:
   A  B  C           
30       3.393492224 
31 1  0  13          
32 2  0  15          
33 3  0  14          
34 4  1  13          
35 5  0  10          
36 6  1  12          
37 7  0  12          
38 8  3  18          
39 9  0  15          
40 10 2  8           
41 11 2  11          
42 12 3  8           
43 13 2  6           
44 14 3  4           
45 15 2  12          
46 16 0  11          
47 17 0  10          
48 18 1  14          
49 19 0  14          
50 20 0  8           
Sheet1
[Table-It] version 09 by Erik Van Geit
Code:
RANGE   FORMULA (1st cell)
B31:B50 {=FREQUENCY(C31:C50,A31:A50)}°
C30     =STDEV(C31:C50)
C31:C50 =VLOOKUP(RAND(),D$4:E$23,2)
{=formula}°:
 select entire range
 enter formula without {}
 confirm with Control-Shift-Enter
[Table-It] version 09 by Erik Van Geit
Note that the plot of the integers will more closely resemble the plot of the PDF as the number of integers increases.
For 30 integers that can be randomly selected according to the PDF from 1 to 20:
In Cell B31, edit and CTRL+SHIFT+ENTER:
=FREQUENCY(C31:C60,A31:A50)
This will appear in Cells B31 to B50 as:
{=FREQUENCY(C31:C60,A31:A50)}
In Cell C30
=STDEV(C31:C60)
=3.750249
In Cell C31 (copied down to C60):
=VLOOKUP(RAND(),D$4:E$23,2)
Code:
   A  B  C  
31 1  0  11 
32 2  0  9  
33 3  1  8  
34 4  1  7  
35 5  2  14 
36 6  1  10 
37 7  3  11 
38 8  3  5  
39 9  3  12 
40 10 2  3  
41 11 6  11 
42 12 1  4  
43 13 2  11 
44 14 2  15 
45 15 1  7  
46 16 0  19 
47 17 1  7  
48 18 0  5  
49 19 1  6  
50 20 0  10 
51       14 
52       9  
53       17 
54       11 
55       13 
56       11 
57       8  
58       13 
59       9  
60       8  
Sheet1
[Table-It] version 09 by Erik Van Geit
Code:
RANGE   FORMULA (1st cell)
B31:B50 {=FREQUENCY(C31:C60,A31:A50)}°
C31:C60 =VLOOKUP(RAND(),D$4:E$23,2)
{=formula}°:
 select entire range
 enter formula without {}
 confirm with Control-Shift-Enter
[Table-It] version 09 by Erik Van Geit
Note that the series of integers will not correspond exactly to the PDF used to generate them.
The resulting average and STD will vary from the target.
The input STD can be varied to adjust the output STD.
Note that for INPUT STD = 4 THE OUTPUT STDEV OR STDEVP TENDS TO BE CLOSE TO 4.
This is because not much of the tailing curve is cut off on both sides of the plot.
For higher input, for example STD = 10, output STDEV = 5.427 for one simulated run with 200 integers selected from 1 to 20.
For STD = 10 much of the INPUT PDF is cut off on both sides of the plot.
Note that RAND() function recalculates with every cell change.
Note that for very large input STD the chance of selecting every integer is almost exactly the same.
 
Upvote 0
Wooov great, thank you very much.



I made the excel file and it's ok. I can change SD and mean, but is it possible to set min and max? Let's say I need 50 positive generated random integers from 1 to 300.
 
Last edited:
Upvote 0
Re: Positive random number generator from Normal distribution

susuru:

The previous post was just an example of how this could work.
To make it more easily adjustable things should be moved around.
This has been done.

Here are directions on using this.
To change settings for the number (15) selected from 1 to 25:

1) Enter numbers in Cells F1 to F3 and STD and AVG
Cell F1: First integer in sequence (1 in this example)
Cell F2: How many integers in sequence (25 in this example)
Cell F3: How many random integers (15) are selected from sequence (1 to 25 in this example)
Cell D4: Enter STD
Cell D5: Enter AVG

Note this is 1 to 25 instead of 1 to 300

2) Copy formulas in Cells A5 to D5 in Row 5 down 25 Rows
Formulas, including the ones in A5 to D5, automatically adjust.

3) Enter array formula starting in Cell E5 going down 25 Rows
The formula in E5 is a special case.
It can not be simply copied down rows.

To enter the formula in E5, or to modify it, first delete it from Cells E5 down the column, and then:
Select Cells E5 through E29 (25 cells)
Enter Edit Mode (F2 key)
Paste formula (with Control Shift Enter):
=FREQUENCY(INDIRECT("F5:F"&F$3+4),INDIRECT("D5:D"&F$2+4))

4) Copy formula in Cell F5 down 15 Rows
Excel Workbook
ABCDEF
10.0357428940.035804817STD4Start1
20.0350207410.035081413AVG13End25
30.9982705351Select15
4NORMDISTNORMALIZENORMsum# Integers154.472135955
50.0011079620.00110988201012
60.0022733910.0022773290.0011098822014
70.0043820750.0043896670.0033872113013
80.0079349130.007948660.007776878408
90.0134977420.0135211260.015725538509
100.021569330.0216066980.029246664626
110.0323793990.0324354950.0508533617020
120.0456622710.0457413790.0832888568117
130.0604926810.0605974820.1290302369114
140.0752843580.0754147850.18962771810014
150.0880163320.0881688170.26504250311013
160.0966670290.0968345010.3532113212212
170.099735570.0999083580.45004582113223
180.0966670290.0968345010.5499541791436
190.0880163320.0881688170.6467886815016
200.0752843580.0754147850.734957497161
210.0604926810.0605974820.810372282171
220.0456622710.0457413790.870969764180
230.0323793990.0324354950.916711144190
240.021569330.0216066980.949146639201
250.0134977420.0135211260.970753336210
260.0079349130.007948660.984274462220
270.0043820750.0043896670.992223122231
280.0022733910.0022773290.996612789240
290.0011079620.0011098820.998890118250
Sheet1
Excel 2003
Cell Formulas
RangeFormula
A1=STDEV(INDIRECT("A5:A"&F2+4))
A2=STDEVP(INDIRECT("A5:A"&F2+4))
A3=SUM(INDIRECT("A5:A"&F2+4))
A5=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A6=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A7=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A8=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A9=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A10=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A11=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A12=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A13=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A14=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A15=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A16=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A17=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A18=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A19=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A20=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A21=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A22=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A23=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A24=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A25=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A26=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A27=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A28=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
A29=NORMDIST(ROW()-5+F$1,D$2,D$1,0)
B1=STDEV(INDIRECT("B5:B"&F2+4))
B2=STDEVP(INDIRECT("B5:B"&F2+4))
B3=SUM(INDIRECT("B5:B"&F2+4))
B5=A5/A$3
B6=A6/A$3
B7=A7/A$3
B8=A8/A$3
B9=A9/A$3
B10=A10/A$3
B11=A11/A$3
B12=A12/A$3
B13=A13/A$3
B14=A14/A$3
B15=A15/A$3
B16=A16/A$3
B17=A17/A$3
B18=A18/A$3
B19=A19/A$3
B20=A20/A$3
B21=A21/A$3
B22=A22/A$3
B23=A23/A$3
B24=A24/A$3
B25=A25/A$3
B26=A26/A$3
B27=A27/A$3
B28=A28/A$3
B29=A29/A$3
E4=SUM(INDIRECT("E5:E"&F2+4))
F4=STDEV(INDIRECT("D5:D"&F3+4))
F5=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F6=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F7=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F8=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F9=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F10=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F11=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F12=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F13=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F14=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F15=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F16=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F17=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F18=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
F19=VLOOKUP(RAND(),INDIRECT("C$5:D$"&F$2+4),2)
C5=IF(ROW()=5,0,SUM(B4:B$5))
C6=IF(ROW()=5,0,SUM(B5:B$5))
C7=IF(ROW()=5,0,SUM(B$5:B6))
C8=IF(ROW()=5,0,SUM(B$5:B7))
C9=IF(ROW()=5,0,SUM(B$5:B8))
C10=IF(ROW()=5,0,SUM(B$5:B9))
C11=IF(ROW()=5,0,SUM(B$5:B10))
C12=IF(ROW()=5,0,SUM(B$5:B11))
C13=IF(ROW()=5,0,SUM(B$5:B12))
C14=IF(ROW()=5,0,SUM(B$5:B13))
C15=IF(ROW()=5,0,SUM(B$5:B14))
C16=IF(ROW()=5,0,SUM(B$5:B15))
C17=IF(ROW()=5,0,SUM(B$5:B16))
C18=IF(ROW()=5,0,SUM(B$5:B17))
C19=IF(ROW()=5,0,SUM(B$5:B18))
C20=IF(ROW()=5,0,SUM(B$5:B19))
C21=IF(ROW()=5,0,SUM(B$5:B20))
C22=IF(ROW()=5,0,SUM(B$5:B21))
C23=IF(ROW()=5,0,SUM(B$5:B22))
C24=IF(ROW()=5,0,SUM(B$5:B23))
C25=IF(ROW()=5,0,SUM(B$5:B24))
C26=IF(ROW()=5,0,SUM(B$5:B25))
C27=IF(ROW()=5,0,SUM(B$5:B26))
C28=IF(ROW()=5,0,SUM(B$5:B27))
C29=IF(ROW()=5,0,SUM(B$5:B28))
D5=ROW()-5+F$1
D6=ROW()-5+F$1
D7=ROW()-5+F$1
D8=ROW()-5+F$1
D9=ROW()-5+F$1
D10=ROW()-5+F$1
D11=ROW()-5+F$1
D12=ROW()-5+F$1
D13=ROW()-5+F$1
D14=ROW()-5+F$1
D15=ROW()-5+F$1
D16=ROW()-5+F$1
D17=ROW()-5+F$1
D18=ROW()-5+F$1
D19=ROW()-5+F$1
D20=ROW()-5+F$1
D21=ROW()-5+F$1
D22=ROW()-5+F$1
D23=ROW()-5+F$1
D24=ROW()-5+F$1
D25=ROW()-5+F$1
D26=ROW()-5+F$1
D27=ROW()-5+F$1
D28=ROW()-5+F$1
D29=ROW()-5+F$1
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.

Directions for plotting two charts:

Create two named ranges Insert Name Define:
Norm1 =INDIRECT("Sheet1!B$5:B$"&Sheet1!$F$2+4)
Norm2 =INDIRECT("Sheet1!E$5:E$"&Sheet1!$F$2+4)

Create two charts with plot ranges of:
=NormDistSimulate.xls!Norm1
=NormDistSimulate.xls!Norm2
Where 'NormDistSimulate' is the name of the Excel file.

The first chart plots the PDF and the second plots the randomly generated data.

Ideas for further improvements:
The filling of the rows with formulas could be automated with a macro.
The histogram could also be done with the Excel Histogram function instead of with the Frequency function.
You would need to look at whether the Frequency or Histogram is easier to automate with a macro.
 
Upvote 0
susuru:

I should make a correction as this may be confusing to someone who may look at this later.

CORRECTION:
The formulas previously in Cell A1 to B2 have been deleted as these do not mean anything and the STD of the input PDF is predefined anyway.
The formula in Cell F4 has been corrected from:
=STDEV(INDIRECT("D5:D"&F3+4))
to:
=STDEVP(INDIRECT("F5:F"&F3+4))

An AVERAGE formula has been added in Cell F3

Things in the first 3 rows have been rearranged.

I can imagine that the normal distribution may in some cases apply to time data.
For instance arrivals, hopefully before, a flight leaves would tend to group maybe an hour ahead of departure.

For the new example below, 8 are selected from 1 to 10.

Here the number in Cell A3 corresponds to 98.8% of the area under the bell curve.
The tails of the bell curve are cut off on both sides, but the closer this number is to 1, or 100%, the more closely this will model the full bell curve.

When adjusting 'End' and 'Select' in Cell D2 and D3, it is a nuisance to have to keep filling in the rows with formulas, and this should be automated with a macro. More of the calculations could probably be done in VBA code also.

There is an easy way around this.
Just fill in the rows with the maximum number that are needed, and then adjust 'Start' and 'End' and 'Select' as needed.
The extra rows will be 'invisible' to the formulas and the charts.
This even applies to the array formula that starts in Cell E5.

Code:
   A           B           C           D          E  F           
 1 STD         2           Start       1                         
 2 AVG         5.5         End         10                        
 3 0.988471605 1           Select      8             5.125       
 4 NORMDIST    NORMALIZE   NORMsum     # Integers 8  2.087911636 
 5 0.015869826 0.016054913 0           1          0  5           
 6 0.043138659 0.043641779 0.016054913 2          2  7           
 7 0.091324543 0.092389647 0.059696692 3          0  7           
 8 0.150568716 0.152324776 0.152086339 4          0  2           
 9 0.193334058 0.195588884 0.304411116 5          3  8           
10 0.193334058 0.195588884 0.5         6          0  5           
11 0.150568716 0.152324776 0.695588884 7          2  5           
12 0.091324543 0.092389647 0.847913661 8          1  2           
13 0.043138659 0.043641779 0.940303308 9          0              
14 0.015869826 0.016054913 0.983945087 10         0              
Sheet1
[Table-It] version 09 by Erik Van Geit
Code:
RANGE   FORMULA (1st cell)
A3      =SUM(INDIRECT("A5:A"&D2+4))
A5:A14  =NORMDIST(ROW()-5+D$1,B$2,B$1,0)
B3      =SUM(INDIRECT("B5:B"&D2+4))
B5:B14  =A5/A$3
C5:C6   =IF(ROW()=5,0,SUM(B4:B$5))
C7:C14  =IF(ROW()=5,0,SUM(B$5:B6))
D5:D14  =ROW()-5+D$1
E4      =SUM(INDIRECT("E5:E"&D2+4))
E5:E14  {=FREQUENCY(INDIRECT("F5:F"&D$3+4),INDIRECT("D5:D"&D$2+4))}°
F3      =AVERAGE(INDIRECT("F5:F"&D3+4))
F4      =STDEVP(INDIRECT("F5:F"&D3+4))
F5:F12  =VLOOKUP(RAND(),INDIRECT("C$5:D$"&D$2+4),2)
{=formula}°:
 select entire range
 enter formula without {}
 confirm with Control-Shift-Enter
[Table-It] version 09 by Erik Van Geit
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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