Random

Description

The random() function generates random numbers within a specified range. It can be used to create random values for various purposes like positioning, sizing, or color components.

Syntax

// Generate random number between 0 and 1
random()

// Generate random number between min and max
random(min: Float, max: Float)

// Generate random integer between min and max (inclusive)
random(min: Int, max: Int)

Parameters

min

Minimum value (optional)

max

Maximum value (optional)

Returns

Returns a random number:

  • Between 0 and 1 when called with no parameters
  • Between min and max when called with two parameters
  • An integer between min and max (inclusive) when called with integer parameters

Example

// Generate random float between 0 and 1
let value = random()  // e.g., 0.234

// Generate random float between 0 and 100
let value = random(0, 100)  // e.g., 42.7

// Generate random integer between 1 and 10
let value = random(1, 10)  // e.g., 7

// Use random values for shape properties
rect(random(0, width), random(0, height), 50, 50)  // Random position
fill(random(0, 255), random(0, 255), random(0, 255))  // Random color
stroke(random(0, 255))  // Random grayscale stroke

Notes

  • The random number generator is seeded automatically for reproducibility
  • When using integer parameters, both min and max are inclusive in the range
  • Useful for creating organic, non-repetitive patterns and animations
  • Can be combined with other functions like noise() for more complex randomization