×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

random number generation

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

RE: random number generation

Code taken from http://www.fontstuff.com/vba/vbatut06.htm

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources