Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Gaussian (normal) random numbers

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gaussian (normal) random numbers

    Include this file, and use nrnd.

    Regards
    Peter
    Code:
    #if not %def(%NRND_INC)
    %NRND_INC = 1
    ' Version 1.0
    ' Peter Stephensen 2002
     
    ' Helper function
    sub nrnd_BoxMuller(y1 as double, y2 as double)
     
        local x1 as double, x2 as double, w as double
         
        do
            x1 = 2 * rnd - 1    
            x2 = 2 * rnd - 1    
            w = x1 * x1 + x2 * x2   
        loop while w >= 1
         
        w = sqr(-2 * log(w) / w)
     
        y1 = x1 * w 
        y2 = x2 * w     
     
    end sub
     
    ' Returns a gaussian random number
    ' Method: Polar form of the Box-Muller transformation (see http://www.taygeta.com/random/gaussian.html)  
    function nrnd() as double
     
        static flag as long
        static y1 as double, y2 as double
     
        if flag then
            flag = 0
            function = y2
        else
            flag = 1
            nrnd_BoxMuller y1, y2
            function = y1
        end if
     
    end function
     
    #endif ' %NRND_INC
    ------------------
    psp@dreammodel.dk www.dreammodel.dk

    [This message has been edited by Peter P Stephensen (edited May 16, 2002).]
    psp@dreammodel.dk
    www.dreammodel.dk

  • #2
    ' This is another function to create random values having a
    ' normal distribution with a specified Mean and Standard Deviation (SD).
    '
    ' Best regards
    '
    ' Erik ---- e.chr@email.dk
    Code:
    #COMPILE EXE
    #REGISTER NONE
    #DIM ALL
    
    FUNCTION RandomNormalValue(Mean AS DOUBLE,SD AS DOUBLE) AS DOUBLE
        LOCAL PI2# : PI2 = 6.2831853071796
        FUNCTION = SQR(-2 * LOG(RND)) * COS(PI2 * RND) * SD + Mean
    END FUNCTION
     
    FUNCTION PBMAIN
        LOCAL I&,X#,N#,M#,S#,S2#,Mean#,SD#,t$
        ' You can specify the following values according to your need
        RANDOMIZE 9.8
        Mean = 150
        SD   = 20
        N    = 400000
        t$ = "Criteria:    Mean "+STR$(Mean) + "     SD " + STR$(SD) + $CRLF + $CRLF
        t$ = t$ + "For"+STR$(N)+ " normally distributed random numbers the actual parameters are:"+ $CRLF + $CRLF
        FOR I = 1 TO N
            X = RandomNormalValue(Mean,SD)
            M = M + X
            S2 = S2 + X * X
        NEXT
        S = SQR((S2 - M * M / N) / N)
        M = M / N
        MSGBOX t$ + "Mean "+FORMAT$(M,"#####.#####")+"     SD "+FORMAT$(S,"#####.#####"),,"Report:"
    END FUNCTION
    ------------------

    Comment


    • #3
      Hi Erik

      The polar form of the Box-Muller transformation is considered both faster and more robust numerically (see the link in my post)

      Regards
      Peter


      ------------------
      psp@dreammodel.dk
      www.dreammodel.dk
      psp@dreammodel.dk
      www.dreammodel.dk

      Comment


      • #4
        Hi Peter,

        You are of course right. The function presented by you is to be
        preferred. I only present the other function and the one below
        to illustrate some ideas. I hope you don't mind.

        Best regards,

        Erik
        Code:
        FUNCTION RandomNormalValue2(Mean AS DOUBLE,SD AS DOUBLE) AS DOUBLE
            ' The distribution of sums of many small random numbers will be normal.
            ' This function is slow, but it illustrates Gauss' idea.
            LOCAL I&, R#
            FOR I& = 1 TO 48
                R = R + RND
            NEXT
            FUNCTION = Mean + 0.5 * (R - 24) * SD
        END FUNCTION

        ------------------




        [This message has been edited by Erik Christensen (edited June 03, 2002).]

        Comment

        Working...
        X