Construct 3 icon

Construct 3

Documentation

Advanced random

Published 20 Nov, 2018
772 words
~3-5 mins

The Advanced Random object provides expressions for advanced pseudo-random number generation (PRNG), including two- and three-dimensional noise functions like Perlin noise (referred to as "classic noise" in the plugin). These are useful for procedural generation, such as providing an endless supply of interesting and unique level designs.

It also provides seeded random functions, which provide the same pseudo-random number sequence when given the same seed. This can also be used to override the system random function (covering the random() expression, and any randomness used in behaviors) with a seeded random, which can provide deterministic random number generation for the whole runtime. By default the seed is always itself random, meaning random number generation is different between different runs of the game.

Click here to open an example of the Advanced Random plugin. This uses the Drawing Canvas object to display randomly generated textures using Advanced Random.

Gradients

The Advanced Random object also provides tools to calculate custom gradients. This is useful for procedural generation. The random numbers it generates are in the range 0-1, but gradients allow you to specify values or colors for certain ranges of that. For example making the range 0-0.5 blue, and the range 0.5-1 green, allows you to look up the color using a random number in the range 0-1 and get either a water or land color. Combined with expressions like 2D perlin noise, this provides a way to randomly generate a level with mixed regions of water and land.

Advanced random properties

Seed

A string of characters used as the seed for random number generation. The same seed will always provide the same sequence of numbers. An empty string (the default) will use a random seed, ensuring the sequence of numbers is different every time.

Replace system random

If enabled, the system random function, which covers the random system expression as well as randomness in behaviors, is overridden to use the Advanced Random object's PRNG. Since the Advanced Random object can control the seed, this provides a way to seed the random number generation of the entire runtime.

Advanced random conditions

The Advanced Random object has no conditions.

Advanced random actions

Add gradient stop

Adds a stop to the current gradient. Use after Create gradient to specify the gradient. The stop position can be any number, but is generally kept within the 0-1 range so it can be used with the random expressions. The stop value should be an expression of the form rgbEx(r, g, b) or rgba(r, g, b, a) when the gradient uses color mode; otherwise it can be a simple number. The default gradient is a simple black to white gradient, using rgbEx(0, 0, 0) at position 0 and rgbEx(100, 100, 100) at position 1.

Create gradient

Create a new gradient. Multiple gradients can be managed by giving them different names. Creating a gradient also sets it as the current gradient, so this action can be immediately followed by Add gradient stop to specify the gradient. By default gradients work in color mode, which uses values based on the rgbEx or rgba expressions; however they can also be set to number mode which uses simple numbers.

Set gradient

Set the current gradient by its name. This allows switching between multiple gradients.

Set octaves

Set the number of octaves used for coherent noise generation, from 1-16. The default is 1. This affects the Billow, Classic and Ridged expressions only. Using additional octaves adds layers of increasing detail to the noise functions, but is also slower to process.

Update seed

Set a new seed for random number generation, using a string.

Advanced random expressions

Billow2d(x, y)

Billow3d(x, y, z)

Generate a random number using billow noise in the range 0-1, using either 2D or 3D co-ordinates.

Cellular2d(x, y)

Cellular3d(x, y, z)

Generate a random number using cellular noise in the range 0-1, using either 2D or 3D co-ordinates.

Classic2d(x, y)

Classic3d(x, y, z)

Generate a random number using classic (perlin) noise in the range 0-1, using either 2D or 3D co-ordinates.

Gradient(Position)

Sample the currently set gradient at the given position. This returns a color value for color mode gradients, otherwise a simple number.

RandomSeed

Generate a random seed string that can be used to set the seed, restoring an unpredictable number sequence.

Ridged2d(x, y)

Ridged3d(x, y, z)

Generate a random number using ridged noise in the range 0-1, using either 2D or 3D co-ordinates.

Seed

The currently set seed, as a string.

Voronoi2d(x, y)

Voronoi3d(x, y, z)

Generate a random number using Voronoi noise in the range 0-1, using either 2D or 3D co-ordinates.