random
On this page

random

Function

A seeded hash produces a noise-like scalar from its input. Repeated seeds in a mathematical model give repeated outputs.

Compare examples

The display shows the fractional part of a sine hash for different seeds. These are illustrative CPU calculations.

Example 1fract(sin(0 × 12.9898) × 43758.5453123)
Result
0
Example 2fract(sin(1 × 12.9898) × 43758.5453123)
Result
0.9216954
Example 3fract(sin(2 × 12.9898) × 43758.5453123)
Result
0.05722738

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

When to use it

Use it for visual noise where cross-device equality is unnecessary.

Inputs and limits

This compiler uses a sine hash whose GPU result varies by driver. The shown numbers are illustrative CPU calculations, not reproducible GPU random values or cryptographic randomness.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns an f32 in the range [0, 1) hashed from an f32, vec2 or vec3 seed, emitted as fract(sin(dot(seed, k)) * 43758.5453123).

Syntax

declare function random(seed: f32 | vec2 | vec3): f32

Parameters

Return value

f32

Description

One seed is one value in the IR and NOT on a GPU: WGSL bounds sin to 2^-11 absolute error on [-PI, PI] and not at all outside it, which is where this hash lives, so the driver decides the answer (surface section 55 has the measurements; #181 replaces the hash with an exact integer one). There is no unseeded form: Math.random() without a seed does not compile.

Examples

See also

Edit this page Report a problem