Random

Sometimes you will want to mix things up and surprise the player either by generating random numbers or choosing randomly.

Choosing Randomly

Choosing randomly is best to choose randomly from a set of options, such as a random character class or random object. If you want to generate a number between a range, see Generating Random Numbers below.

If you want to choose randomly between 16 or less selections, then you should probably use choose() instead of random(). choose() is better when used to select random names, messages or other strings and when the numbers to be chosen are not consecutive.

Generating Random Numbers

Game Maker has two functions and one action for generating things randomly. They are :

Using Random Numbers

Random Seed

Game Maker cannot generate true random numbers because it is impossible for any computer to generate a truly random number. Instead, random numbers are generated by applying math to a seed number. Game Maker's random seed is set automatically when a game is loaded and there is no need to set the random seed before generating random numbers as with some game programming languages. However, if you ever want to generate the same set of random numbers, the functions below can help you.

Random Position

Random Movement

  • Move Fixed - can start or change an instance moving randomly.

Random Speed

Random speed of an object :

// replace min_speed and max_speed
// with your actual minimum and maximum speeds

speed = random(min_speed + max_speed) - min_speed;

Tutorials

Related Pages

Actions

GML

Backlinks

Categories: Math : Random
page tags: math random