Normally Distributed Random Numbers - SLUniverse Forums
 
Navigation » SLUniverse Forums > Development Discussion and Support > Scripting > Script Library » Normally Distributed Random Numbers


Script Library Post useful scripts here.

Reply
 
LinkBack Thread Tools Display Modes
Old 09-30-2009, 09:46 AM   #1 (permalink)
Fortuna vitrea est
 
Tyche Shepherd's Avatar
Warming her pearls
 
Join Date: Aug 2007
Location: UK
Posts: 5,827
My Mood:
SL Join Date: 26th April 2007

Awards: 1
Best Anglo-Saxon High Horse Reference 
Normally Distributed Random Numbers

Earlier today I commented on a JIRA request to implement an LSL function to return normally distributed random numbers with a known mean and standard deviation ([#SVC-4865] llRandN(float mean, float standard_devation) - Second Life Issues) My comment was that whilst it would be a nice feature it's a trivial thing to code in LSL . I thought I'd better put my money where my mouth was.

The LSL llFrand returns numbers which are uniformly distributed like this:


This routine returns numbers which are Normally distributed around a known mean and standard deviation. So the distribution looks more like this (i.e. values close to the mean are more likely to be selected):


And here is the code:
Code:
integer use_stored;
float stored;
float gaussian_random(float mean,float stddev)
  /*
       Routine to generate pseudo-random numbers from a normal distribution with a specific mean and standard deviation
        parameters 
           mean : Mean of source distribution 
           stddev : Standard Deviation of the source distribution
        Uses Polar form of the Box-Muller transformation function
	Since this method generates two random numbers on first call we generate both and use the first,
	Then on second call we use the stored second random variable
	and set use_stored to 0 so that 2 more variables are generated on next call
	Author: Tyche Shepherd
	Date: 2009-09-30
		
	*/
{
	float rand1;
	float rand2;
	float first;
	float factor;
		
	if(use_stored==1){
           //use second number and then reset
	   use_stored=0;	
           return mean+(stddev * stored);
        } else {
	    factor=1.1;
	    while((factor>=1.0) || (factor==0)){
		rand1=(2.0*llFrand(1.01))-1.0;
		while(rand1>1.0){
  		  rand1=(2.0*llFrand(1.01))-1.0;
		}
		rand2=(2.0*llFrand(1.01))-1.0;
		while(rand2>1.0){
  		  rand2=(2.0*llFrand(1.01))-1.0;
		}
		  factor=(rand1*rand1)+(rand2*rand2);
	    }
	    factor=llSqrt((-2.0*llLog(factor))/factor);
	    first=rand1*factor;
	    //store second number and set flag to use this next time
	    stored=rand2*factor;
	    use_stored=1;
	    return mean+(stddev*first);
       }
}
default
{

	state_entry()
	{
       //set initial state of use_stored so that first call generates new numbers
        use_stored=0;   

	}
	touch_start(integer total_number){
	    //just a stub to show how to call the routine
		float value;
		//generate a normal distributed random number from a distribution with mean 100 and std.dev of 10 
		value=gaussian_random(100.0,10.0);
		llSay(0,"Random Number is " +(string) value);
	}

}
Uses: Anywhere where you need random values which are normally distributed. Particles and/or prim movement are possible areas where this is useful .
License: Do what you want with the code and if it breaks anything remember that tyche didn't do this !
__________________

Vanguard of the LolCatz Revolution
This Post was financed by The National LolCatz Archives

Clancy Sullivan :Yeah. YEAH! The sultry seamstress of mirth is definitely in charge now.
Certified 7.8 on the Official Non-Arbitrary Trout Algorithmic Slut scale

A public copy of my Second Life Main Grid Survey Database can be found at http://www.gridsurvey.com - Now with added Second Life Incidents !!



Last edited by Tyche Shepherd; 09-30-2009 at 06:17 PM.
Tyche Shepherd is offline   Reply With Quote
1 User Said Yay!:
1 User Said Thanks:
Old 09-30-2009, 10:12 AM   #2 (permalink)
Emergency Mustelid
 
Argent Stonecutter's Avatar
 
Join Date: Sep 2009
Posts: 2,614
You're sure that compiles? Functions are supposed to be defined at the global level.
Argent Stonecutter is online now   Reply With Quote
1 User Said Thanks:
Old 09-30-2009, 10:35 AM   #3 (permalink)
Fortuna vitrea est
 
Tyche Shepherd's Avatar
Warming her pearls
 
Join Date: Aug 2007
Location: UK
Posts: 5,827
My Mood:
SL Join Date: 26th April 2007

Awards: 1
Best Anglo-Saxon High Horse Reference 
Quote:
Originally Posted by Argent Stonecutter View Post
You're sure that compiles? Functions are supposed to be defined at the global level.
Sorry you are right - I just wrote and tested it using Lsleditor which doesn't seem to mind where the function is and not in world . I've now rewritten it and tested in world - Original has now been changed
Tyche Shepherd is offline   Reply With Quote
Reply

Tags
gaussian distribution, random numbers, too geeky for most, tycharrific, tyche didn't do this

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On