Random

From AGI Wiki
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:

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

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:
random(0, 10, v50);
print("%v50 is greater than or equal to 0 "
      "AND less than or equal to 10");

Technical Information

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

See Also

Mathematical Commands