irandom

Returns a random integer between 0 and n. This includes n if n itself is an integer value.

Syntax:

irandom(n);


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


Returns: Integer


Description

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... real numbers can also be used but the upper value 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 generates the same initial random seed every time to make debugging code a far easier task. To avoid this behaviour use randomize at the start of your game.


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.


Back: Real Number Functions
Next: irandom_range
© Copyright YoYo Games Ltd. 2018 All Rights Reserved