Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number generation

Status
Not open for further replies.

simone73

Petroleum
Aug 22, 2003
2
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
 
Replies continue below

Recommended for you

Code taken from
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
 
In the case of a continuous variable, it's not all that hard to "roll your own" generating function.

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor