| Script Library Post useful scripts here. |
| |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Fortuna vitrea est ![]() ![]() ![]() ![]() ![]()
Warming her pearls
| 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);
}
}
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. |
| | |
| 1 User Said Yay!: |
| 1 User Said Thanks: |
![]() |
| Tags |
| gaussian distribution, random numbers, too geeky for most, tycharrific, tyche didn't do this |
| Thread Tools | |
| Display Modes | |
| |