Capítulo 18 de 988

Chapter 18: random()

Core Idea

The random() API will give deterministic pseudorandom values between 0 and 1. Unlike the Math.

Key Concepts

  • Use cases
  • Accessing true randomness
  • Compatibility
  • See also

Code Examples

const rand = random(1); // 0.07301638228818774
const rand2 = random(1); // still 0.07301638228818774

const randomCoordinates = new Array(10).fill(true).map((a, i) => {
  return {
    x: random(`random-x-${i}`),
    y: random(`random-y-${i}`),
  };
}); // will always be [{x: 0.2887063352391124, y: 0.18660089606419206}, ...]

// @ts-expect-error
random(); // Error: random() argument must be a number or a string
  • What it demonstrates: Usage pattern for random() from the official docs.

Key Takeaways

  1. Understand use cases when working with random().
  2. Understand accessing true randomness when working with random().
  3. Understand compatibility when working with random().
  4. Understand see also when working with random().

Connects To

  • Absolute Fill: related page in the Fundamentals & Core Concepts section.
  • Animating Properties: related page in the Fundamentals & Core Concepts section.
  • Animation Math: related page in the Fundamentals & Core Concepts section.