メインコンテンツまでスキップ

srandom: Bipolar Random Generation

Balanced Distribution Functions for Symmetric Patterns

Signed random functions generate values in the range [1,1][-1, 1] instead of the typical [0,1][0, 1]. 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:

srandom(x)=2random(x)1\text{srandom}(x) = 2 \cdot \text{random}(x) - 1

This shifts the [0,1][0, 1] range to [1,1][-1, 1] while preserving the uniform distribution properties. For vector outputs, the same transformation applies component-wise.

Special vector functions use enhanced mixing coefficients:

srandom2(p)=2fract(16kfract(pxpy(px+py)))1\text{srandom2}(p) = 2 \cdot \text{fract}(16k \cdot \text{fract}(p_x \cdot p_y \cdot (p_x + p_y))) - 1

Where k=(0.3183099,0.3678794)k = (0.3183099, 0.3678794) provides improved spatial distribution.

Function Variants

FunctionInputOutputPurpose
srandomfloatfloatSigned 1D random
srandomVec2vec2float2D to signed scalar
srandomVec3vec3float3D to signed scalar
srandom2Vec2vec2vec2Enhanced 2D vector
srandom3Vec3vec3vec3Enhanced 3D vector
srandom3Vec3Tiledvec3, floatvec3Tileable 3D vector

Implementation

ライブエディター
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)
}
ライブエディター
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)
}