Sine–Fract Hash (canonical GLSL one-liner)

also: canonical one-liner GLSL rand, fract-sin hash, one-liner hash

No first-appearance record exists: the one-liner has no known author or original publication and was already widely diffused across the web by 2013 (to be confirmed — likely earlier).

unattributed Name — Genuinely unattributed. The function circulates by copy-paste and is identified by its magic constants (12.9898, 78.233, 43758.5453), not by a name or an author: a working graphics programmer investigating it in 2013 could not find who first wrote it despite its appearance "in dozens of places on the internet" (see references). It achieved folk-canonical status through copy-paste diffusion rather than publication or citation. The descriptive handle used here ("Sine–Fract Hash") is an index label for a reference-track record, NOT a claim that the technique has an established name or a known author — per the two-track rule (SPEC §7.4), the index records the verifiable procedure while leaving authorship openly unknown.

In one line The de-facto pseudo-random hash of shader code: a one-liner that maps a 2D coordinate to a value in [0,1) as fract(sin(dot(p, k)) * m). Cheap, requires no texture or state, and is used as the workhorse randomness source inside fragment shaders (dithering, value/gradient noise, stippling, jitter). It is identified in practice not by a name or an author but by its magic constants.

The canonical GLSL hash as noise — raw per-pixel hash (left) vs. bilinear value noise (right) · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

sine-fract-hash/index.html
// The de-facto GLSL random hash, shown as noise. Left half: the raw per-pixel
// hash (uniform static). Right half: value noise built by bilinearly
// interpolating that same hash on an integer lattice. Both use the *canonical
// constants* verbatim — the technique's real identifier. Seeded per load.
const SEED = Math.random();
const c = document.getElementById('c');
const dpr = Math.min(devicePixelRatio || 1, 2);
const W = Math.min(innerWidth, innerHeight * 16 / 9);
c.width = Math.floor(W * dpr);
c.height = Math.floor(W * 9 / 16 * dpr);
const gl = c.getContext('webgl');
if (!gl) { c.outerHTML = '<p style="color:#fff;font-family:system-ui">WebGL unavailable</p>'; }
const vs = `attribute vec2 a; void main(){ gl_Position = vec4(a, 0., 1.); }`;
// highp + the canonical one-liner. Note (see the record's how_it_works): this
// leans on the hardware sin(); on some mobile GPUs (e.g. Adreno 320) sin() is
// imprecise for large arguments and the noise degrades — highp mitigates it.
const fs = `precision highp float;
uniform vec2 u_res; uniform float u_seed;
// canonical GLSL rand — magic constants verbatim
float rand(vec2 co){ return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); }
float valueNoise(vec2 p){
  vec2 i = floor(p), f = fract(p);
  f = f * f * (3.0 - 2.0 * f);            // smoothstep fade
  float a = rand(i);
  float b = rand(i + vec2(1.0, 0.0));
  float d = rand(i + vec2(0.0, 1.0));
  float e = rand(i + vec2(1.0, 1.0));
  return mix(mix(a, b, f.x), mix(d, e, f.x), f.y);
}
void main(){
  vec2 uv = gl_FragCoord.xy / u_res;      // 0..1
  vec2 s = vec2(u_seed * 91.7, u_seed * 47.3);
  float v;
  if (uv.x < 0.5) {
    // raw hash: one value per pixel -> TV-static
    v = rand(floor(gl_FragCoord.xy) + s);
  } else {
    // value noise: the same hash, bilinearly smoothed on a lattice
    v = valueNoise(gl_FragCoord.xy / 22.0 + s);
  }
  // a thin divider down the middle
  float div = smoothstep(0.0015, 0.0, abs(uv.x - 0.5));
  vec3 col = mix(vec3(v), vec3(0.15, 0.55, 0.95), div);
  gl_FragColor = vec4(col, 1.0);
}`;
function sh(t, src){ const o = gl.createShader(t); gl.shaderSource(o, src); gl.compileShader(o);
  if(!gl.getShaderParameter(o, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(o)); return o; }
const prog = gl.createProgram();
gl.attachShader(prog, sh(gl.VERTEX_SHADER, vs));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, fs));
gl.linkProgram(prog); gl.useProgram(prog);
const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]), gl.STATIC_DRAW);
const loc = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
gl.uniform2f(gl.getUniformLocation(prog, 'u_res'), c.width, c.height);
gl.uniform1f(gl.getUniformLocation(prog, 'u_seed'), SEED);
gl.viewport(0, 0, c.width, c.height);
gl.drawArrays(gl.TRIANGLES, 0, 6);

Open the demo source ↗

How it works

The canonical form is: float rand(vec2 co){ return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); } Step by step: take the dot product of the input coordinate with a fixed vector (12.9898, 78.233) to collapse 2D → 1D; apply sin() to fold that value into [-1, 1] with rapid, high-frequency variation; multiply by a large constant (43758.5453) to blow up tiny input differences; and take fract() to keep only the chaotic fractional part, yielding an apparently-random value in [0, 1). The "magic constants" are the technique's de-facto identifier. It is not a principled hash — it relies on the hardware's sin() implementation, so results differ across GPUs and it degrades badly where sin() is imprecise for large arguments (e.g. some mobile GPUs), which is why practitioners add highp precision and pre-reduce the sine argument.

Parameters

Lineage

Related
Perlin / Simplex Noise, Curl Noise, Dithering

Works using this algorithm (0)

References

Last updated 2026-07-16