Skip to main content

Random

The Random class provides utility methods for generating various types of random values, including integers, floating-point numbers, bits, and colors.

static integer(min: number, max: number): number

Generates a random integer between min (inclusive) and max (inclusive).

Parameters:

  • min: The minimum value (inclusive).
  • max: The maximum value (inclusive).

Example:

const value = Random.integer(1, 10);
console.log(value);

static float(min: number, max: number, precision?: number): number

Generates a random floating-point number between min (inclusive) and max (exclusive).

Parameters:

  • min: The minimum value (inclusive).
  • max: The maximum value (exclusive).
  • precision (optional): The number of decimal places (default is 2).

Example:

const value = Random.float(0, 1, 3);
console.log(value);

static bit(): number

Generates a random bit (0 or 1).

Example:

const b = Random.bit();
console.log(b);

static color(hex?: boolean): string | THREE.Color

Generates a random color optionally in hexadecimal format.

Parameters:

  • hex (optional): If true, returns the color in hexadecimal format; otherwise, returns a THREE.Color object.

Example:

const colorObj = Random.color(); // THREE.Color
const hexColor = Random.color(true); // '#aabbcc'

static vector3(min: number, max: number, float?: boolean): THREE.Vector3

Generates a random THREE.Vector3 object.

Parameters:

  • min: The minimum value for each component (x, y, z).
  • max: The maximum value for each component (x, y, z).
  • float (optional): If true, generates floating-point values; otherwise, integers.

Example:

const vec = Random.vector3(0, 10);
console.log(vec.x, vec.y, vec.z);

static vector2(min: number, max: number, float?: boolean): THREE.Vector2

Generates a random THREE.Vector2 object.

Parameters:

  • min: The minimum value for each component (x, y).
  • max: The maximum value for each component (x, y).
  • float (optional): If true, generates floating-point values; otherwise, integers.

Example:

const vec2 = Random.vector2(0, 5);
console.log(vec2.x, vec2.y);