Difference between revisions of "LlFrand"
Gally Young (talk | contribs) m (remove multi lang) |
(Added some examples for random integer generation) |
||
Line 13: | Line 13: | ||
|caveats=The random number generator is not a source of entropy. | |caveats=The random number generator is not a source of entropy. | ||
|examples=<lsl> | |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: Drop by the minimum possible float value to avoid the outlier and then let | |||
// the integer cast do the rest | |||
integer fast_random_integer( integer min, integer max ) | |||
{ | |||
return min + (integer)( llFrand( max - min - 0.0000001 ) ); // Hg | |||
} | |||
default | default | ||
{ | { | ||
Line 19: | Line 53: | ||
// When touched, say "Heads" with probability 0.5, | // When touched, say "Heads" with probability 0.5, | ||
// otherwise, say "Tails." | // otherwise, say "Tails." | ||
if ( | if ( coin_toss() ) | ||
llSay(0, "Heads"); | llSay(0, "Heads"); | ||
else | else | ||
llSay(0, "Tails"); | 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> | </lsl> | ||
|helpers=[[Pseudo-random_Number_Generator]] - Suitable for apps which require repeatable results that feel random. | |helpers=[[Pseudo-random_Number_Generator]] - Suitable for apps which require repeatable results that feel random. |
Revision as of 02:21, 16 December 2008
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: float llFrand( float mag );8 | Function ID |
0.0 | Forced Delay |
10.0 | Energy |
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
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: Drop by the minimum possible float value to avoid the outlier and then let
// the integer cast do the rest
integer fast_random_integer( integer min, integer max ) {
return min + (integer)( llFrand( max - min - 0.0000001 ) ); // Hg
}
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 ); }
}
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 |