SC2Mapster.com Wiki

IntToString

native int RandomInt(int min, int max);

Generate a random integer

Parameters

int min

Minimum value possible to be generated

int max

Maximum value possible to be generated

Return value

int - A random integer between the min an max value. If max < min then the boundaries are swapped.

Examples

Example #1: Sample Generated Numbers
int i;
i = RandomInt(-1, 1); // i = -1;
i = RandomInt(-1, 1); // i = 1;
i = RandomInt(-1, 1); // i = 1;
i = RandomInt(-1, 1); // i = 0;
i = RandomInt(-1, 1); // i = -1;
Example #2: Min > Max
i = RandomInt(10, 5); // i = 6;
// Is equivalent to RandomInt(5, 10);
Example #3: Min = Max
i = RandomInt(5, 5); // i = 5;
Example #4: Sample Usage
if (RandomInt(1, 7) == 7) {
  // We have 1/7 chance to enter this condition
}
if (RandomInt(1, 7) <= 5) {
  // We have 5/7 chance to enter this condition
}
Example #5: Custom probability check
bool RandomChance(fixed percent) {
  if (percent >= 1.0) {
    return true;
  } else if (percent <= 0.0) {
    return false;
  } else {
    return RandomInt(1, 4096) <= FixedToInt(percent * 4096);
  }
}

if (RandomChance(1.0 / 3.0))
{
    // this will only execute one third of the time
}

You must login to post a comment. Don't have an account? Register to get one!

Facts

Date created
Mar 09, 2010
Last updated
Mar 10, 2010

Authors