Difference between revisions of "Random Gaussian Number Generator"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "<lsl> float randGauss(float mean=0.,float stdev=1.){ float V1,V2,S; do{ V1=2*llFrand(1.)-1; V2=2*llFrand(1.)-1; S=V1*V1+V2*V2; }while (S>=1); return mean+stdev*V1*llSqr…")
 
m (<lsl> tag to <source>)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
<lsl>
Port of the Random Gaussian algorithm found on http://www.taygeta.com/random/gaussian.html<nowiki>.</nowiki>
float randGauss(float mean=0.,float stdev=1.){
 
float V1,V2,S;
<source lang="lsl2">float randGauss(float mean, float stdev){
do{
    float x, y, r2;
V1=2*llFrand(1.)-1;
    do{//Generate a point in a unit circle that is not zero.
V2=2*llFrand(1.)-1;
        x = llFrand(2.) - 1;
S=V1*V1+V2*V2;
        y = llFrand(2.) - 1;
}while (S>=1);
        r2 = x * x + y * y;
return mean+stdev*V1*llSqrt(-2*(llLog(S)/S));
    } while (r2 > 1.0 || r2 == 0);
}
 
</lsl>
    //Box-Muller transformation
    return mean + x * stdev * llSqrt( -2 * llLog(r2) / r2);
}</source>
 
<source lang="lsl2">vector randGaussPair(vector center, float stdev){//2D
    //returns a random point on the x/y plain with a specified standard deviation from center.
    float r2;
    vector p;
    do{//Generate a point in a unit circle that is not zero.
        p = <llFrand(2.) - 1, llFrand(2.) - 1, 0>;
        r2 = p * p;//dot product
    } while (r2 > 1.0 || r2 == 0);
 
    //Box-Muller transformation
    return center + (p * (stdev * llSqrt( -2 * llLog(r2) / r2)));
}</source>
 
==Box-Muller Transformation==
The Box-Muller transformation is used to adjust the magnitude of the vector, remapping it to a standard deviation.
 
==3D==
Is this correct? Or does Box-Muller need to be adjusted?
<source lang="lsl2">vector randGaussPoint(vector center, float stdev){//3D
    //returns a random point with a specified standard deviation from center?
    float r2;
    vector p;
    do{//Generate a point in a unit sphere that is not zero.
        p = <llFrand(2.) - 1, llFrand(2.) - 1, llFrand(2.) - 1>;
        r2 = p * p;//dot product
    } while (r2 > 1.0 || r2 == 0);
 
    //Box-Muller transformation
    return center + (p * (stdev * llSqrt( -2 * llLog(r2) / r2)));
}</source>

Latest revision as of 07:43, 25 January 2015

Port of the Random Gaussian algorithm found on http://www.taygeta.com/random/gaussian.html.

float randGauss(float mean, float stdev){
    float x, y, r2;
    do{//Generate a point in a unit circle that is not zero.
        x = llFrand(2.) - 1;
        y = llFrand(2.) - 1;
        r2 = x * x + y * y;
    } while (r2 > 1.0 || r2 == 0);

    //Box-Muller transformation
    return mean + x * stdev * llSqrt( -2 * llLog(r2) / r2);
}
vector randGaussPair(vector center, float stdev){//2D
    //returns a random point on the x/y plain with a specified standard deviation from center.
    float r2;
    vector p;
    do{//Generate a point in a unit circle that is not zero.
        p = <llFrand(2.) - 1, llFrand(2.) - 1, 0>;
        r2 = p * p;//dot product
    } while (r2 > 1.0 || r2 == 0);

    //Box-Muller transformation
    return center + (p * (stdev * llSqrt( -2 * llLog(r2) / r2)));
}

Box-Muller Transformation

The Box-Muller transformation is used to adjust the magnitude of the vector, remapping it to a standard deviation.

3D

Is this correct? Or does Box-Muller need to be adjusted?

vector randGaussPoint(vector center, float stdev){//3D
    //returns a random point with a specified standard deviation from center?
    float r2;
    vector p;
    do{//Generate a point in a unit sphere that is not zero.
        p = <llFrand(2.) - 1, llFrand(2.) - 1, llFrand(2.) - 1>;
        r2 = p * p;//dot product
    } while (r2 > 1.0 || r2 == 0);

    //Box-Muller transformation
    return center + (p * (stdev * llSqrt( -2 * llLog(r2) / r2)));
}