Don't click here unless you want to be banned.

LSL Wiki : randomVector

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl836.us.archive.org
Still work in progress.
It compiles, and the little bits are good.

//////
//    OK, so randBetween is from the llFrand page
//    we can use it to generate a random number from  minimum to maximum.
//    You will see how it works in the example below
////    and yes, I flip around with color and colour - in my comments its colour, in code, I concede to LL

float randBetween(float min, float max)
{
    return llFrand(max - min) + min;
}

//////
//  Colours are expressed in vectors. 
//   So, below gives us a basic random colour generator

vector randColor()
{   
    return <llFrand(1.0), llFrand(1.0), llFrand(1.0)>;    //sends back a message to whoever asks the random colour
}

//////
//  Taking it one step further
//   So, below gives us a basic random colour generator

vector randVector()
{                         //HERE we use the randBetween we have up on top to 
                           //make a random number from 100 to 500 - on the Z axis. Fun for a trampoline :) 
    return <0,0,randBetween(100, 500)>;    
}

default
{
    touch_start(integer total_number)
    {
        vector tempRandomColor = randColor();  //this loads temp ariable with random colur
        llSay(0, (string)tempRandomColor );    //this says the vector, just to know its working
        
        llSetColor(tempRandomColor, ALL_SIDES); //set colour
                          ///here we push the person who touched cube, 
        llPushObject( llDetectedKey(0), randVector(), ZERO_VECTOR, 0);
    } 
}

back to functionLibrary
Comments [Hide comments/form]
Although this function will give you a random vector, the vector that you receive will be biased towards the corners of the cube. To see this, imagine that you are at 0,0,0 in a cube with sides at 1 and -1 in all axes. If you look straight at a center, you would see that it took 1 unit before you exited the cube. If you look at a corner, it would take you sqrt(3) which is about 1.732. So, that direction would get chosen that much more. For some uses, that would be good enough.

If it isn't, there's an easy way and a hard way to fix it. The easy way is to see if the length returned is greater than 1, if so, then try again. This means it will take more tries to get a vector, but it shouldn't be too many. If you don't like the fact that it will take variable time, then you need the hard way.

The hard way is to generate only unit vectors, this means they would all have a distance of exactly one. I am still learning LSL, so this will be pseudo-code, but I hope it will be easy to follow.

You need to generate 2 random numbers, Z from -1 to 1 and theta from 0 to 2 * PI (or 360 degrees * DEG_TO_RAD, if that's easier to remember) Z is the height on the sphere, with 0=equator. theta is the angle around the circle to use. First you need to find the size of the circle with that Z value. It is: radius = sqrt(1 - z*z), this follows from the Pythagorean Theorem aka distance formula. The X and Y locations are made by using the circle formula X = radius * cos(theta), Y = radius * sin(theta).

This formula, although complex will give you the best results, as all directions on the sphere are the same probability. If you want to have the inside of the sphere, then multiply the vector (X,Y,Z) found times a random value 0 to 1. As a stab at the LL code
vector unitVector()
{
float z = randBetween(-1,1);
float theta = randBetween(0, 2 * PI);
float distance = llSqrt(1 - z * z);
return <distance * cos(theta), distance * sin(theta), z>
}
-- adsl-75-25-180-5.dsl.pltn13.sbcglobal.net (2007-11-24 22:25:01)
Oh, since I haven't logged in, I should add that the previous commenter is DeeDee Littlething in world.
-- adsl-75-25-180-5.dsl.pltn13.sbcglobal.net (2007-11-24 22:27:22)
Attach a comment to this page: