srandom: Bipolar Random Generation
Balanced Distribution Functions for Symmetric Patterns
Signed random functions generate values in the range instead of the typical . This balanced distribution creates more natural symmetric patterns and eliminates the bias toward positive values common in standard random functions.
Mathematical Foundation
The transformation from standard random to signed random applies the linear mapping:
This shifts the range to while preserving the uniform distribution properties. For vector outputs, the same transformation applies component-wise.
Special vector functions use enhanced mixing coefficients:
Where provides improved spatial distribution.
Function Variants
Function | Input | Output | Purpose |
---|---|---|---|
srandom | float | float | Signed 1D random |
srandomVec2 | vec2 | float | 2D to signed scalar |
srandomVec3 | vec3 | float | 3D to signed scalar |
srandom2Vec2 | vec2 | vec2 | Enhanced 2D vector |
srandom3Vec3 | vec3 | vec3 | Enhanced 3D vector |
srandom3Vec3Tiled | vec3, float | vec3 | Tileable 3D vector |
Implementation
Live Editor
const fragment = () => { const p = uv.mul(6) const flow = srandom2Vec2(p.floor()) const gradient = dot(p.fract().sub(0.5), flow) const intensity = gradient.mul(2).clamp(-1, 1) return vec4(intensity.step(0), intensity.abs(), intensity.mul(-1).step(0), 1) }
Live Editor
const fragment = () => { const coord = vec3(uv.mul(4).floor(), 0) const direction = srandom3Vec3(coord) const strength = direction.length() const color = direction.mul(0.5).add(0.5).mul(strength) return vec4(color, 1) }