random number generation
random number generation
(OP)
is there a macro that generate a random extraction from a particular probability distribution and put that number in a cell so it can be recalculated?
thanks
thanks
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS Come Join Us!Are you an
Engineering professional? Join Eng-Tips Forums!
*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail. Posting GuidelinesJobs |
|
RE: random number generation
Public Function RandomNumbers(Lowest As Long, Highest As Long, _
Optional Decimals As Integer)
Application.Volatile 'Remove this line to "freeze" the numbers
If IsMissing(Decimals) Or Decimals = 0 Then
Randomize
RandomNumbers = Int((Highest + 1 - Lowest) * Rnd + Lowest)
Else
Randomize
RandomNumbers = Round((Highest - Lowest) * Rnd + Lowest, Decimals)
End If
End Function
dongxiao PEng
http://www.cadtool.net
RE: random number generation
Start with the variable's Probability Density Function, PDF(x), and construct its corresponding Cumulative Distribution Function, CDF(x):
CDF(x) = Integral of PDF(x) with respect to x from negative infinity to x. This CDF will monotonically increase from 0 at negative infinity to 1 at positive infinity.
In many cases you will already have a formula for the CDF, and in cases where you do not have it you can probably construct an adequate piecewise-linear approximation to it.
Once you have the CDF, you have what is, in effect, a transformation between a uniform distribution over (0,1) and your required PDF. And Excel's RAND() function generates the former. What you are actually going to use is the mathematical inverse of this CDF.
Graphically what you must do programatically is:
(1) Use RAND() to generate a random number between zero and one.
(2) Mark this value as the ordinate of your CDF.
(3) Construct a horizontal line at that ordinate value, and intersect it with the CDF curve.
(4) Read off the abscissa value that corresponds to the intersection point.
(5) The set of abcissa values so generated will be distributed according to your initial PDF.
HTH