+ Reply to Thread
Results 1 to 4 of 4

Random Number Generation

Hybrid View

vioravis Random Number Generation 02-25-2009, 08:38 PM
solnajeff Re: Random Number Generation 02-25-2009, 09:13 PM
shg Re: Random Number Generation 02-25-2009, 09:17 PM
vioravis Re: Random Number Generation 02-27-2009, 08:39 PM
  1. #1
    Registered User
    Join Date
    02-09-2005
    Posts
    79

    Random Number Generation

    I am trying to generate random numbers with the the constraint that the maximum value is 40 and the most likely value is around 40% of the maximum value. Are there any distributions (lognormal??) that would suit this requirement? Is there any way I can do this in Excel? Thanks a lot.
    Last edited by vioravis; 02-27-2009 at 10:01 PM.

  2. #2
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Re: Random Number Generation

    Hi

    If you are generating integers then there is a relatively straightforward way to generate 'random' or 'semi random' numbers by creating a list of the number range you are interested in and using VBA to choose your random number. A simple example is attached, the example usaes the RAND function to generate a number alongside each member of the set and then uses the MAX function to determine the highest random number and then uses the set member alongside. This can be used with whatever sample is appropriate and substituted accordingly. There are many other ways to achieve the same result if something else would be more appropriate.

    If each number must be generated only once then some additinal code is required, please advise if this is the case.

    Regards

    Jeff
    Attached Files Attached Files

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Random Number Generation

    This generates Log-Normal random variates. The second function generates normal variates and is called by the first.
    Const BigPos    As Single = 3.402823E+38
    Const BigNeg    As Single = -BigPos
    Const SmlPos    As Single = 1.401298E-45
    
    Function RandLogNorm(fMu As Single, _
                         fSigma As Single, _
                         Optional bVolatile As Boolean = False) As Variant
    
        ' shg 2008-1230
        
        ' Returns a pair of Log-Normal random variates
        ' http://en.wikipedia.org/wiki/Log-normal_distribution
        '   fMu Real
        '   fSigma > 0
        ' mean = exp(fMu + fSigma^2/2)
        ' var = (exp(fSigma^2) - 1) * exp(2*fMu + fSigma^2)
    
        Dim afRN()  As Single   ' pair of random normal variates
        
        If bVolatile Then Application.Volatile
    
        afRN = RandNorm()
        RandLogNorm = Array(Exp(fMu + fSigma * afRN(0)), _
                            Exp(fMu + fSigma * afRN(1)))
    End Function
    
    Function RandNorm(Optional Mean As Single = 0!, _
                      Optional Dev As Single = 1!, _
                      Optional fCorrel As Single = 0!, _
                      Optional fMin As Single = BigNeg, _
                      Optional fMax As Single = BigPos, _
                      Optional bVolatile As Boolean = False) As Single()
    
        ' shg 1999-1103
        ' rev 2008-1205 to add min and max
    
        ' Returns a pair of random deviates (Singles) with the specified
        ' mean, deviation, and correlation. Orders of magnitude faster than
        '     =NORMINV(RAND(), Mean, Dev)
    
        ' Box-Muller Polar Method
        ' Donald Knuth, The Art of Computer Programming,
        ' Vol 2, Seminumerical Algorithms, p. 117
    
        Dim z(0 To 1) As Single
        Dim u       As Single
        Dim v       As Single
        Dim s       As Single
    
        If bVolatile Then Application.Volatile
        If fMax < fMin Then fMax = fMin
    
        Do
            Do
                u = 2! * Rnd - 1!
                v = 2! * Rnd - 1!
                s = u ^ 2 + v ^ 2
            Loop Until s < 1!
    
            s = Sqr(-2 * Log(s) / s)
            z(0) = Dev * u * s + Mean
            z(1) = Dev * v * s + Mean
        Loop Until WorksheetFunction.Max(z) >= fMin And _
             WorksheetFunction.Min(z) <= fMax
    
        If fCorrel <> 0! Then z(1) = fCorrel * z(1) + Sqr(1! - fCorrel ^ 2) * z(2)
        RandNorm = z
    End Function
    EDIT: I also have one for skew-normal variates if that's what you want ...
    Last edited by shg; 02-26-2009 at 01:55 AM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    02-09-2005
    Posts
    79

    Re: Random Number Generation

    Thanks a lot guys. Finally had to do it without using Excel

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1