Random

From AGI Wiki
Revision as of 07:25, 19 April 2019 by Andrew Korson (talk | contribs) (Created page with "The '''random''' command generates a random number within a specified range. == Syntax == random(byt LOWER, byt UPPER, var vRESULT); == R...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The random command generates a random number within a specified range.

Syntax

random(byt LOWER, byt UPPER, var vRESULT);

Remarks

The value of variable vRESULT is set to a pseudo random number between LOWER and UPPER inclusive. The algorithm that AGI uses to generate random numbers is:

<syntaxhighlight lang="agi">if (rndseed == 0)

 {
 rndseed = current_clock_count; [ use internal clock count as starting seed
 }

rndseed = (rndseed * 0x7C4D) mod 0xFFFF + 1; vRESULT = LOWER + rndseed mod (UPPER - LOWER + 1);</syntaxhighlight>

Possible Errors

To work correctly, UPPER must be greater than or equal to LOWER. If If LOWER is greater than UPPER, then the returned value will be in the range 0 - 255. If LOWER is exactly = UPPER + 1, it causes a divide by zero situation, which will crash AGI.

Example

Code:

<syntaxhighlight lang="agi"> random(0, 10, v50); print("%v50 is greater than or equal to 0 "

     "AND less than or equal to 10");

</syntaxhighlight>

Technical Information

Required Interpreter Version: Available in all AGI versions.
Byte-Code Value: 130 (0x82 hex)

See Also

Mathematical Commands