irandom

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

Syntax:

GML Code Example

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:

GML Code 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.





No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here