random() (Game Maker)

( Game Maker, 6.1, 7 )

random - generate a random number

Syntax

rnum = random(x)

  • rnum = returned random real number
  • x = upper limit of random number.

Description

random() generates a random real number between 0 and less than x. It is produced as a decimal fraction, so if you want a whole number (integer), you should use floor() or ceil() in combination with random(). floor() is used most often.

Examples

Simple Example

After the code below is executed, rnum} will contain a random real number between 0 and less than 5. Remember, this number will be a number with a decimal point and not an integer.

rnum = random(5)

50/50 Chance

A 50/50 chance can be simulated this way :

if (random(1) >= .5)
{
    //something happens
}

Simulating Dice Rolls

Standard 6-sided dice and other polyhedral dice rolls can be simulated by simply using random() to generate a number between 1 and the maximum number of the die. Don't forget the + 1.

die = floor(random(6)) + 1

Related Pages

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.