irandom

This very useful function only returns integers (whole numbers). So, for example, to get a random number from 0 to 9 you can use irandom(9) and it will return a number from 0 to 9 inclusive. Note that floats can also be used but the upper value after the point will be excluded, so irandom(9.7) will return a value from 0 to 9 only. The function has an upper bound of $7fffffffffffffffLL, so care should be taken if using very large numbers.

NOTE: This function will return the same value every time the game is run afresh due to the fact that GameMaker Studio 2 generates the same initial random seed every time to make debugging code a far easier task. To avoid this behaviour use randomise() at the start of your game.

 

Syntax:

irandom(n);

Argument Description
n The upper range from which the random number will be selected.

 

Returns:

Integer

 

Example:

if (irandom(9) == 1)
   {
   score += 100;
   }

This will produce a one in ten (since 0 is included) chance of adding 100 to the score.