LlFrand/ru

From Second Life Wiki
Jump to navigation Jump to search

Общая информация

Function: float llFrand( float mag );

Возвращает вещественное псевдослучайное число в интервале [0.0, mag) (если mag положительно) или (mag, 0.0] (если mag отрицательно).[1]

• float mag Любое вещественное значение

При переводе вещественных чисел в целые следует применять операцию типового преобразования (integer) , а не функции округления (llRound, llFloor, llCeil). (Понятия не имею, почему --прим. пер.)

Предостережения

  • Данный генератор случайных чисел не является абсолютным.
    • Последовательность случайных чисел постоянна для всего процесса. Следовательно, данный генератор неприменим для задач, в которых требуется полностью предсказуемый или непредсказуемый результат.
  • Следует помнить, что при задании целочисленного значения на месте mag оно будет преобразовано в вещественный тип. При задании целого числа вне отрезка [-223, +223] оно может быть неправильно воспринято программой (вследствие ограничений вещественного типа). Аналогично, использование данной функции для генерирования случайного целого числа выдаст результат, содержащий не больше 24 случайных бит.
All Issues ~ Search JIRA for related Bugs

Примеры

<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 surprisingly tricky and emotive subject // and has caused endless discussion on the scripting groups. // The primary cause of probability errors when employing llFrand is to have a varying bin size on the edges of the range. // // As the bracket notation indicates, [0.0, mag), the function is inclusive of the 0.0 and exclusive of the entered value. // Because an LSL floating point number is only a subset of real numbers and does not have infinite granularity, this schema // will work for any float greater than float t = 1.175494351e-38; at which value the function will // return only zero. At a float beyond this, a math error occurs.

// Random integer generator // Contributed by Mephistopheles Thalheimer, original function posited by Hg Beeks

// Returns a pseudo-random integer in the range of min to max inclusive.

// Rationale: Expands the range by 1.0 to ensure equal bin spacing on ends relative to the middle of // the range and then uses an integer cast to round towards zero.

// Caveats: This function is not range checked and will fail if max < min

integer 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 );

   }

}</lsl>

<lsl>// Simple integer random number tester // Contributed by Mephistopheles Thalheimer

// This is a random number tester designed to give a quick visual explanation and proof of why some // random integer functions just do not work. // In general, with any random number generator, if you can see a pattern emerging, then chances are, // the function is not random.

// The test case given "silly_random_integer( .. )" shows the type of pitfalls that can happen. Superficially, // it would seem like a good candidate. I thought so, and in fact mooted it in a discussion, however, a bit of thought reveals // that the first and last bin are only collecting rounded results from half the float space as the rest of the integers. // They are therefore under-represented in output, and the generator is flawed.


integer random_integer( integer min, integer max ) {

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

}

integer silly_random_integer( integer min, integer max ) {

 return min + (integer)( llRound( llFrand( max - min ) ) );  // Looks good, but does not work

}


// Simple integer random number tester // Contributed by Mephistopheles Thalheimer

list bins;

integer MIN = 2; // The minimum integer you want integer MAX = 5; // The maximum integer you want

integer NUMBER_OF_TRIES = 10000; // The bigger the better.. but slower

default {

   state_entry()
   {
       llSay( PUBLIC_CHANNEL, "Bin tester ready.");
       bins = [];
   }
   touch_start(integer total_number)
   {
       
       llSay( PUBLIC_CHANNEL, "Started, be patient" );
       
       integer i;
       integer r;
       
       integer range = MAX - MIN;
       
       for( i = 0; i <= range; ++i )
       {
           bins += [ 0 ];    
       }
       
       integer v;
       integer out_of_range;
       
       for( i = 0; i < NUMBER_OF_TRIES; ++i )
       {
           r = silly_random_integer( MIN, MAX );   // Replace this with the function you are testing
                                                   // Note the output on this one has about 0.5 expected hits on the first and last bin
           //r = random_integer( MIN, MAX );
           
if( r > MAX

Полезные вырезки

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

Заметки

More Random

As stated, llFrand() provides a pseudo-random number (not truly random, but it "feels" like it). For a more truly random number, you can create a "seed factor" by creating a random number of random numbers prior to the actual final generation. Example: <lsl>//FOR AN INCREASINGLY RANDOM NUMBER: // creates a significantly "more random" random number integer loop=0; integer count=(integer) llFrand(50); // generate a random number of loops while(loop <= count){llFrand(1);++loop;} // generate random numbers within the loop integer actual_number=llFrand(100)+1; // generate the final truly-random number </lsl> This causes a random(50) number of random numbers to be created prior to your actual final random number, significantly increasing the random factor in arriving at your final result.

См. также

Функции

•  llListRandomize

Углублённые заметки

Search JIRA for related Issues

Сноски

  1. ^ Математические промежутки описаны в статье [1].

Описания

function float llFrand( float mag );