Difference between revisions of "LlFrand"

From Second Life Wiki
Jump to navigation Jump to search
(Added some examples for random integer generation)
Line 37: Line 37:


   
   
// Hg: Drop by the minimum possible float value to avoid the outlier and then let
// Hg: While it IS technically possible to get the maximum value in llFrand and cause this to return a value above the range,
// the integer cast do the rest
// the likelyhood of this occuring is 1:1,000,000 for every whole number in the range - You will have a 1:5,000,000 chance of
// getting 5 if doing llFrand(5), at the very least likelyhood. Testing also seems to imply that llFrand goes past the six-digit
// lead that float returns, which means that the likelyhood could, in fact, be lower than this. However, decreasing
// the +1 to .999999 in fact affects the probability and would make the highest value you want just a smidgen less likely.


integer fast_random_integer( integer min, integer max )
integer fast_random_integer( integer min, integer max )
{
{
   return min + (integer)( llFrand( max - min - 0.0000001 ) ); // Hg
   return min + (integer)llFrand( max - min + 1 );
}
}
   
   



Revision as of 02:42, 16 December 2008

Summary

Function: float llFrand( float mag );

Returns a float that is pseudo random number in the range [0.0,mag) or (mag, 0.0].
The sign of mag matches the return.

• float mag Any valid float value

Specification

returns a pseudo random number in range [0.0, mag) or (mag, 0.0], depending upon the sign of mag.

Caveats

The random number generator is not a source of entropy.

All Issues ~ Search JIRA for related Bugs

Examples

<lsl>

// Tosses a coin, giving a *near* 50:50 chance of a result.

integer coin_toss() {

 if( llFrand(1.) < .5 ) return TRUE;
 return FALSE;

}


// Sometimes it is useful to get a random integer over a given range. This is a suprisingly tricky and emotive subject // and has caused endless discussion on the groups. Here are some solutions.


// Meph: Expand the range by 1.0 to ensure equal bin spacing on ends and in the middle of the range // and then the integer cast catches the negative outlier (-0.5) when it rounds towards zero.

integer random_integer( integer min, integer max ) {

 // Caution, not range checked
 return min + (integer)( llRound( llFrand( max - min + 1 ) - 0.5 ) );  

}


// Hg: While it IS technically possible to get the maximum value in llFrand and cause this to return a value above the range, // the likelyhood of this occuring is 1:1,000,000 for every whole number in the range - You will have a 1:5,000,000 chance of // getting 5 if doing llFrand(5), at the very least likelyhood. Testing also seems to imply that llFrand goes past the six-digit // lead that float returns, which means that the likelyhood could, in fact, be lower than this. However, decreasing // the +1 to .999999 in fact affects the probability and would make the highest value you want just a smidgen less likely.

integer fast_random_integer( integer min, integer max ) {

 return min + (integer)llFrand( max - min + 1 );

}


default {

   touch_start(integer total_number)
   {
       // When touched, say "Heads" with probability 0.5, 
       // otherwise, say "Tails."
       if ( coin_toss() )
           llSay(0, "Heads");
       else
           llSay(0, "Tails");

       integer n1 = random_integer( 2, 8 ); // Return a random number between 2 and 8
       llSay( PUBLIC_CHANNEL, "I chose a " + (string)n1 );

       integer n2 = fast_random_integer( 3, 10 ); // Returns a (slightly faster) random integer between 3 and 10 
       llSay( PUBLIC_CHANNEL, "I chose a " + (string)n2 );

   }

}


</lsl>

Useful Snippets

Pseudo-random_Number_Generator - Suitable for apps which require repeatable results that feel random.

Notes

The random number generator is not a source of entropy.

The sequence of random numbers are shared across the entire process, and not independently seeded. Therefore, the pseudo random number generation is not suitable for any application which requires completely predictable or completely unpredictable results.

See Also

Functions

•  llListRandomize

Deep Notes

Search JIRA for related Issues

Signature

function float llFrand( float mag );