What's that shader random function doing?

December 4, 2026

The majority of the time, computers don't actually do anything random. Often, instead, you end up using a pseudorandom number generator, which produces seemingly uncorrelated numbers in sequence starting from a seed.

In p5.js, if you call randomSeed(0) at the start of a sketch that uses random() a few times, you'll get the same result every time you run it. If you switch the seed, e.g. randomSeed(1), you get a different sequence of random values, but which again will be the same every time you run it.

let seed

function setup() {
  createCanvas(200, 200)
  seed = createInput('0', 'number')
  seed.input(() => redraw())
  noLoop()
}

function draw() {
  randomSeed(parseInt(seed.value()))
  background(255)
  noStroke()
  fill(0)
  for (let i = 0; i < 2000; i++) {
    circle(
      random(width),
      random(height),
      2
    )
  }
}

Shaders generally don't work this way. Every pixel runs the same program, all at the same time, in parallel. If I include a random seed, that means every pixel's run starts fresh from that same seed. If I were to set the pixel color to a random tone, I'd get the same result on every pixel, and end up with a solid color! Below is not a real shader, but a simulation of what it would be like:

let seed

function setup() {
  createCanvas(200, 200)
  pixelDensity(1)
  seed = createInput('0', 'number')
  seed.input(() => redraw())
  noLoop()
}

function draw() {
  loadPixels()
  for (let x = 0; x < width; x++) {
    for (let y = 0; y < height; y++) {
      randomSeed(parseInt(seed.value()))
      let v = random(255)
      pixels[(y * width + x) * 4] = v
      pixels[(y * width + x) * 4 + 1] = v
      pixels[(y * width + x) * 4 + 2] = v
      pixels[(y * width + x) * 4 + 3] = 255
    }
  }
  updatePixels()
}

Ok, maybe we want a different seed per pixel, then. While every pixel runs the same program, they are able to have different inputs, such as their coordinate on canvas. Let's use the coordinate to offset the random seed, effectively incrementing the seed each pixel.

let seed

function setup() {
  createCanvas(200, 200)
  pixelDensity(1)
  seed = createInput('0', 'number')
  seed.input(() => redraw())
  noLoop()
}

function draw() {
  loadPixels()
  let seedOffset = 0
  for (let x = 0; x < width; x++) {
    for (let y = 0; y < height; y++) {
      randomSeed(parseInt(seed.value()) + x * width + y)
      let v = random(255)
      pixels[(y * width + x) * 4] = v
      pixels[(y * width + x) * 4 + 1] = v
      pixels[(y * width + x) * 4 + 2] = v
      pixels[(y * width + x) * 4 + 3] = 255
    }
  }
  updatePixels()
}

None of these shader simulations appear random. If each pixel were totally uncorrelated from each other, I'd expect a screen that looks like white noise.

The extreme non-randomness is because our usual random number functions are designed for the CPU: seed once, and then each subsequent random number is as uncorrelated as possible from the previous. It was not designed for each random seed to be completely uncorrelated from the next.

So what do shaders do instead? They have random functions that are designed differently. There is no distinction between the seed and the following random calls. Instead, it works like a hash function: you put in an input, and it maps it to an output that is as uncorrelated as possible to the input. The input may be a vector, such as the pixel coordinate (or, if you need multiple random numbers per pixel, a vec3 with the pixel coordinate plus an index that ticks up each random call.)

Here's what that looks like, running in a real shader this time:

let randomShader

function setup() {
  createCanvas(200, 200)
  pixelDensity(1)
  randomShader = buildFilterShader(() => {
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(sin(dot(coord, vec2(12.9898, 78.233))) * 43758.5453123)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  filter(randomShader)
}

Ok, that looks like white noise! Now what on earth is fract(sin(dot(coord, vec2(12.9898, 78.233))) * 43758.5453123)??

Where did it come from?

When searching for "glsl random" Google's AI overview currently writes this function and cites The Book of Shaders for popularizing it. The Book of Shaders is great, check it out! It's not where it came from, though: as far as I can tell from archive.org, it started in 2015. At least one StackOverflow answer using the same snippet was posted in 2010.

There's actually a whole other StackOverflow post trying to figure out its origin. It mentions a commonly cited early version of this formula with familiar constants in an old tutorial for the Lumina shader development environment. The general formula structure, older still, appears to originate in a 1998 paper, On generating random numbers, with help of y=[(a+x)sin(bx)] mod 1 by W.J.J. Rey, although this seems to be known from its citations in other papers; the original does not seem to be easily accessible online. But its title does appear quite similar in form! fract is basically mod 1 here. Although it isn't a 100% match, this is a likely source, as snippets tend to morph and evolve as they are adapted in different places.

Ok, so what does it actually do, and what are all those magic numbers?

Breaking it down

Rather than writing it as a formula, let's think of it as a pipeline so we can see how values get transformed at each step, and what we expect each value to be.

Step Preview Range Notes
coord {0-1} × {0-1} Since at this point it's a vector, I'm using the red channel to visualize x and green channel to visualize y. It is very ordered to start: we increase steadily in the x and y axes.
dot with vec2(12.9898, 78.233) A single number between 0 and 12.9898+78.233, which is 91.2228 We've turned a vector into a single number. For the unfamiliar, dot([a, b], [c, d]) is equivalent to a*c + b*d. Because in this scenario, c > d, the x value of the incoming coord changes the output more than the y value does. I've scaled down the output values so that they don't immediately clip past 1 and so you can see its gradient.
sin -1 to 1 Here I've scaled the preview so that -1 is black and 1 is white. The result is still pretty ordered. The output of sin will repeat every TWO_PI, or around 6.28, so with the range of the previous input, we're seeing around 15 cycles.
* 43758.5453123 -43758.5453123 to 43758.5453123 I've kept the same color scaling as before. It looks similar to before, but with sharp edges. That's because it's still the same shape, but it just goes up way higher, and gets clipped quickly in my color scaling.
fract 0-1 fract, or mod 1, is basically taking the decimals only and leaving the rest. The result looks random because the decimal part of the above is so different when we look at it at each pixel.

fract, aliasing, and patterns

So at the tail end of the pipeline, we're just grabbing the decimals to get a random number between 0 and 1. So if we can make sure the decimals are uncorrelated from pixel to pixel, our output will look like noise. At a high level, by multiplying the sine wave by a huge number, the value changes so much from pixel to pixel, that the decimals could be anything.

Here's a little demo of just sampling the decimals of a sine wave. Drag the slider to change the amplitude from 1 to 10. Try dragging it slowly to the right: you can see it forms a pattern, but the higher it goes, the harder it is to see the pattern. But there are also points where a noticeable pattern will re-emerge for a moment before disappearing again.

let amplitude

function setup() {
  createCanvas(200, 200)
  amplitude = createSlider(1, 10, 1, 0.0001)
  amplitude.input(() => redraw())
  noLoop()
}

function draw() {
  background(255)
  noStroke()
  fill(0)
  let n = 100;
  let a = amplitude.value()
  for (let i = 0; i < n; i++) {
    let x = map(i, -1, n, 0, width)
    let val = a * sin(i / n * 2 * PI)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }
}

Here's the same visualization but where I've drawn a line at the points where the decimals wrap around off the top to the bottom. Those lines get closer together as the amplitude increases.

let amplitude

function setup() {
  createCanvas(200, 200)
  amplitude = createSlider(1, 10, 1, 0.0001)
  amplitude.input(() => redraw())
  noLoop()
}

function draw() {
  background(255)
  fill(0)
  let n = 100;
  let a = amplitude.value()
  stroke('red')
  for (let i = -ceil(a); i <= ceil(a); i++) {
    if (a === 0) continue
    let x = map(asin(i/a), -PI, PI, 0, width)
    line(x, 0, x, height)
    let x2 = map(asin(i/a)+PI, -PI, PI, 0, width) % width
    line(x2, 0, x2, height)
  }
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let val = a * sin(i / n * 2 * PI)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }
}

Ok, one other version of this where the scale is way bigger, going from 10 to 1000. Drag the slider up and the lines get so close together that we're basically looking at a solid red background.

let amplitude

function setup() {
  createCanvas(200, 200)
  amplitude = createSlider(10, 1000, 10, 0.0001)
  amplitude.input(() => redraw())
  noLoop()
}

function draw() {
  background(255)
  fill(0)
  let n = 100;
  let a = amplitude.value()
  stroke('red')
  for (let i = -ceil(a); i <= ceil(a); i++) {
    if (a === 0) continue
    let x = map(asin(i/a), -PI, PI, 0, width)
    line(x, 0, x, height)
    let x2 = map(asin(i/a)+PI, -PI, PI, 0, width) % width
    line(x2, 0, x2, height)
  }
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let val = a * sin(i / n * 2 * PI)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }
}

At that point, the dots look almost random! There is still symmetry in the dots: the left half of the screen looks like the right, but flipped vertically. But within one half, it looks pretty random. Is this because the lines got so close together?

The answer is no, at least not entirely. Let's look at a non-sine function, say, just x, for comparison. First, you'll see that the red lines are evenly spaced where before they concentrate around a few spots. As you increase the amplitude a lot, the red lines continue to fill up the whole screen as before. But the dots always end up in an ordered pattern! So we can't simply say that when the value changes quickly between pixels, we get random decimals: we have an example of the value changing quickly between pixels but without resulting in random decimals.

let amplitude

function setup() {
  createCanvas(200, 200)
  amplitude = createSlider(1, 100, 1, 0.0001)
  amplitude.input(() => redraw())
  noLoop()
}

function draw() {
  background(255)
  fill(0)
  let n = 100;
  let a = amplitude.value()
  stroke('red')
  for (let i = 0; i <= a; i++) {
    let x = map(i, 0, a, 0, width)
    line(x, 0, x, height)
  }
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let val = a * map(i, 0, n, 0, 1)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }
}

fract(amplitude * x) is a sawtooth wave; it repeats at some frequency. When we sample it at a higher rate than that frequency, we can clearly see the sawtooth wave. As we increase the amplitude variable, it wraps around more quickly, and the sawtooth frequency increases. As the frequency of the wave gets higher than the rate we're sampling it at, we end up missing whole parts of the wave, and because it's repetitive, it lands on the next cycle of the wave with a constant offset.

The result is an effect called aliasing. You may have also heard of a visual variant of this called the Moiré pattern, and it is also the result of looking at a high-frequency texture at a low sample rate, producing a banding effect.

We actually want aliasing; the fact that the red lines end up all on top of each other in the earlier example is important. But we don't want the patterned kind that the sawtooth wave gives us. The fact that the red lines aren't regularly spaced in the sine example helps break up the patterns.

There's still the issue of the symmetry though, how do we deal with that? That ends up being a problem with our sampling period. Take a look at what happens to the fract(amplitude * x) sawtooth versus fract(amplitude * sin(x)) when we change the x domain of the graph but use the same number of samples. Previously, we were going from 0-2π, the period of a sine wave. The slider below changes it from 2 to 2.01: a subtle difference, but enough to break the symmetry for the sine variant but not the sawtooth:

let a = 1000
let domain

function setup() {
  createCanvas(400, 200)
  domain = createSlider(2*PI, 2.01*PI, 2*PI, 0.0001)
  domain.input(() => redraw())
  noLoop()
}

function draw() {
  background(255)
  fill(0)
  let n = 100;
  noStroke()

  push()
  fill(200, 200, 255)
  rect(0, 0, width/2, height)
  pop()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let val = a * sin(i / n * domain.value())
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }

  translate(width/2, 0)
  push()
  fill(255, 200, 200)
  rect(0, 0, width/2, height)
  pop()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let val = a * map(i, 0, n, 0, domain.value())
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }
}

Now we have non-symmetric values on the left. My understanding, then, is that we have three requirements here, all important:

  1. the wrap-around frequency is so high that we aren't sampling the pattern itself, we're mostly getting aliasing (achieved by the 43758.5453123 multiplier, but any big number will do);
  2. the wrap-around repetition is not exactly regular, so that the aliasing doesn't produce a discernable pattern (achieved by sin); and
  3. we aren't sampling at a nice multiple of the period, to break any repetition or symmetry we would otherwise see.

Precision fights back

To confirm whether the points above are sufficient, could you replace sin() with some other function with those properties? We can get point 1 but not 2 with just fract(x), but possibly we could fix it by making the result nonlinear? Let's see what fract(sqrt(x)*a) looks like, visualized on top, compared with fract(sin(x)*a) on the bottom:

let amplitude
let noiseShader
let noiseShader2

function setup() {
  createCanvas(400, 400, WEBGL)
  amplitude = createSlider(1, 43758.5453123, 43758.5453123, 0.00001)
  amplitude.input(() => redraw())
  noiseShader = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(pow(dot(coord, vec2(12.9898, 78.233)), 0.5) * a)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noiseShader2 = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(sin(dot(coord, vec2(12.9898, 78.233))) * a)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  push()
  translate(-width/2, -height/2)
  fill(0)
  let n = 100;
  let a = amplitude.value()
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, 200)
    let orig = (a * pow(map(i, 0, n, 0, 1), 0.5))
    let val = orig - floor(orig)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, 200 - 20, 20)
    circle(x, y, 5)
  }

  shader(noiseShader)
  noStroke()
  translate(width*0.75, 100)
  plane(200, 200)
  pop()

  push()
  translate(-width/2, 0)
  fill(0)
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, 200)
    let orig = (a * sin(i/n * 2.1*PI))
    let val = orig - floor(orig)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, 200 - 20, 20)
    circle(x, y, 5)
  }

  shader(noiseShader2)
  noStroke()
  translate(width*0.75, 100)
  plane(200, 200)
  pop()

  line(-width/2, 0, width/2, 0)
}

The dots look effectively random, but in a shader, we still see some patterns! What's going on here?

Well, the amplitude scale I had above was from 1 to 43758.5453123. Let's make the amplitude go up larger, to 1000000, to see if a larger effect emerges:

let amplitude
let noiseShader
let noiseShader2

function setup() {
  createCanvas(400, 400, WEBGL)
  amplitude = createSlider(1, 1000000, 1000000, 0.00001)
  amplitude.input(() => redraw())
  noiseShader = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(pow(dot(coord, vec2(12.9898, 78.233)), 0.5) * a)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noiseShader2 = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(sin(dot(coord, vec2(12.9898, 78.233))) * a)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  push()
  translate(-width/2, -height/2)
  fill(0)
  let n = 100;
  let a = amplitude.value()
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, 200)
    let orig = (a * pow(map(i, 0, n, 0, 1), 0.5))
    let val = orig - floor(orig)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, 200 - 20, 20)
    circle(x, y, 5)
  }

  shader(noiseShader)
  noStroke()
  translate(width*0.75, 100)
  plane(200, 200)
  pop()

  push()
  translate(-width/2, 0)
  fill(0)
  noStroke()
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, 200)
    let orig = (a * sin(i/n * 2.1*PI))
    let val = orig - floor(orig)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, 200 - 20, 20)
    circle(x, y, 5)
  }

  shader(noiseShader2)
  noStroke()
  translate(width*0.75, 100)
  plane(200, 200)
  pop()

  line(-width/2, 0, width/2, 0)
}

Aha! Something funky is indeed going on!

There's actually a secret extra thing going on between each step that I didn't include in the pipeline earlier: after each operation, it gets stored in a float on the GPU. Those don't have infinite precision, so some data can get lost! You can think of it a bit like scientific notation, so 1234 is 1.234×103. You can think of floats as storing a fixed number of decimals in scientific notation. Say that's 3 decimals. If I make the number 1234.5, that's 1.2345×103, which gets stored as 1.235×103, which is 1235. If our next step is to extract just the decimals of the non-scientific notation number, that's a problem: our decimals are gone!

So there's a sneaky fourth requirement we need to add to the rules from above:

  1. The numbers need to stay in a range where we don't lose precision and wipe out the decimals that produce the random aliasing.

fract(sqrt(x)*a) has to evaluate sqrt(x)*a before grabbing the decimals with fract(), and sqrt(x) is basically unbounded. sin(x), on the other hand, cleanly always output a value from -1 to 1.

To limit the range, we can try fract(sqrt(fract(x))*a), and it fixes the issue:

let amplitude
let noiseShader

function setup() {
  createCanvas(400, 200, WEBGL)
  amplitude = createSlider(1, 43758.5453123, 43758.5453123, 0.00001)
  amplitude.input(() => redraw())
  noiseShader = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(pow(fract(dot(coord, vec2(12.9898, 78.233))), 0.5) * a)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  translate(-width/2, -height/2)
  push()
  fill(0)
  let n = 100;
  let a = amplitude.value()
  noStroke()
  let fract = (x) => x - floor(x)
  for (let i = 0; i < n; i++) {
    let x = map(i, 0, n-1, 0, width)
    let orig = (a * pow(fract(map(i, 0, n, 0, 1)), 0.5))
    let val = orig - floor(orig)
    let decimals = val - floor(val)
    let y = map(decimals, 0, 1, height - 20, 20)
    circle(x, y, 5)
  }

  shader(noiseShader)
  noStroke()
  translate(width*0.75, height/2)
  plane(height, height)
  pop()
}

dot and merging two numbers without patterns

There are more magic numbers we haven't talked about yet: dot(coord, vec2(12.9898, 78.233)). What are those doing?

When we were visualizing aliasing with dots before, that was showing a one-dimensional domain: for a given x value, pick a random y. However, in a pixel shader, the input is generally two-dimensional or higher. So we need some way of merging two numbers into one number, and that's where dot comes in. As mentioned earlier, dot(coord, vec2(12.9898, 78.233)) is the same as coord.x * 12.9898 + coord.y * 78.233.

First experiment: what if we make the magic numbers smaller? Drag the slider down to reduce its amplitude down to zero and see what happens along the way.

let amplitude
let noiseShader

function setup() {
  createCanvas(200, 200, WEBGL)
  amplitude = createSlider(0, 1, 1, 0.00001)
  amplitude.input(() => redraw())
  noiseShader = buildFilterShader(() => {
    let a = uniformFloat(() => amplitude.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(sin(dot(coord, vec2(12.9898, 78.233) * a)) * 43758.5453123)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  push()
  shader(noiseShader)
  noStroke()
  plane(height, height)
  pop()
}

It stays pretty random until it gets close to 0. That makes sense; at 0, all input coordinates get mapped to the same singular merged number, 0. At low values, the inputs are so close together that it counteracts the * 43758.5453123 and removes the aliasing that we need. But anywhere, like, halfway through the slider range and up, it looks equally random!

Let's try a different experiment. Do we need decimals on both the x and y multipliers?

let xMult
let yMult
let noiseShader

function setup() {
  createCanvas(200, 200, WEBGL)
  xMult = createSelect().input(() => redraw())
  xMult.option(12.9898)
  xMult.option(12)
  yMult = createSelect().input(() => redraw())
  yMult.option(78.233)
  yMult.option(78)
  noiseShader = buildFilterShader(() => {
    let xm = uniformFloat(() => xMult.value())
    let ym = uniformFloat(() => yMult.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(sin(dot(coord, vec2(xm, ym))) * 43758.5453123)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  push()
  shader(noiseShader)
  noStroke()
  plane(height, height)
  pop()
}

If you make just one or the other have no decimals, it looks fine. But if you make both have no decimals, then you get some very obvious patterns. Why is that?

It has to do with the fact that input values are on a regular grid. Say we use vec2(5, 10), so the input to the rest of the pipeline is 2*x + 4*y. That means that at input pixel vec2(0.1, 0.1), the input to the pipeline is 0.5 + 1 = 1.5. Will we get that same merged value of 1.5 elsewhere on the grid?

Here's a little visualizer. Click anywhere on the canvas, and this will show you the merged value in the top left, and highlight all the pixels that are within 0.0001 of that same merged value. Try changing the magic numbers and seeing what changes.

let noiseShader
let targetX = 100, targetY = 100
let targetViz
let xMult, yMult

function setup() {
  createCanvas(200, 200, WEBGL)
  pixelDensity(1)
  targetViz = createP().position(0, 0)
  xMult = createSelect().input(() => redraw())
  xMult.option(12)
  xMult.option(12.9898)
  yMult = createSelect().input(() => redraw())
  yMult.option(78)
  yMult.option(78.233)
  noiseShader = buildFilterShader(() => {
    let tx = uniformFloat(() => targetX+0.5)
    let ty = uniformFloat(() => targetY+0.5)
    let xm = uniformFloat(() => xMult.value())
    let ym = uniformFloat(() => yMult.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = map(abs(dot(coord, [xm, ym]) - dot([tx/width, ty/height], [xm, ym])), 0, 0.0001, 0, 1, true)
    let outColor = [v, v, v, 1]
    filterColor.set(outColor)
    filterColor.end()
  })
  noLoop()
}

function mouseClicked() {
  if (mouseY > height) return
  targetX = floor(mouseX)
  targetY = floor(mouseY)
  redraw()
}

function draw() {
  background(255)
  let merged = (parseFloat(xMult.value()) * targetX/width + parseFloat(yMult.value()) * targetY/height)
  targetViz.html(round(merged*10000)/10000)
  push()
  shader(noiseShader)
  noStroke()
  plane(height, height)
  pop()
  filter(ERODE)
}

Well, there's your problem. We're getting repeats when we use no decimals. Why is that?

Here's another visualization. Instead of having a fixed threshold of 0.0001, let's make it a slider, so you can increase it and see how many other pixels had very similar merged numbers.

let noiseShader
let targetX = 100, targetY = 100
let targetViz
let xMult, yMult
let threshold

function setup() {
  createCanvas(200, 200, WEBGL)
  pixelDensity(1)
  targetViz = createP().position(0, 0)
  xMult = createSelect().input(() => redraw())
  xMult.option(12)
  xMult.option(12.9898)
  yMult = createSelect().input(() => redraw())
  yMult.option(78)
  yMult.option(78.233)
  threshold = createSlider(0.0001, 0.1, 0.0001, 0.0001)
    .input(() => redraw())
    .position(0, 230)
  noiseShader = buildFilterShader(() => {
    let tx = uniformFloat(() => targetX+0.5)
    let ty = uniformFloat(() => targetY+0.5)
    let xm = uniformFloat(() => xMult.value())
    let ym = uniformFloat(() => yMult.value())
    let thresh = uniformFloat(() => threshold.value())
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = map(abs(dot(coord, [xm, ym]) - dot([tx/width, ty/height], [xm, ym])), 0, thresh, 0, 1, true)
    let outColor = [v, v, v, 1]
    filterColor.set(outColor)
    filterColor.end()
  })
  noLoop()
}

function mouseClicked() {
  if (mouseY > height) return
  targetX = floor(mouseX)
  targetY = floor(mouseY)
  redraw()
}

function draw() {
  background(255)
  let merged = (parseFloat(xMult.value()) * targetX/width + parseFloat(yMult.value()) * targetY/height)
  targetViz.html(round(merged*10000)/10000)
  push()
  shader(noiseShader)
  noStroke()
  plane(height, height)
  pop()
  filter(ERODE)
}

Even with decimals everywhere, increasing the threshold reveals that there were similar merged numbers nearby all along. We just nearly miss them when we add decimals.

Surprise! It's patterned aliasing, back for round two!

Adding decimals means that the period of the merge function doesn't neatly tile with the regular sampling of the input values on the pixel grid. So when you shift up and over, you dont exactly reach another of the same number. You ever so slightly jump over it. And that's enough!

So, in the spirit of continuing to make our own random function using the same principles but with a different implementation, this just means I could use numbers of similar magnitude as long as one of them has some decimals that makes it not tile neatly. How about 123 and 45.678?

let noiseShader

function setup() {
  createCanvas(200, 200, WEBGL)
  noiseShader = buildFilterShader(() => {
    filterColor.begin()
    let coord = filterColor.texCoord
    let v = fract(pow(fract(dot(coord, vec2(123, 45.678))), 0.5) * 43758.5453123)
    filterColor.set([v, v, v, 1])
    filterColor.end()
  })
  noLoop()
}

function draw() {
  background(255)
  filter(noiseShader)
}

Looks good!

The checklist

Ok, to bring everything back together, here the steps we need:

  1. Merge the two input coordinates into one that doesn't align neatly with the input grid
  2. Stick those values into a wave where
    1. the frequency is way higher than the rate we sample it at,
    2. the frequency modulates over time to not repeat nicely,
    3. the period of modulation doesn't neatly align with the input grid either, and
    4. we don't lose much numerical precision when calculating it.

Play around yourself

Here's a version you can modify live. Try incrementally rewriting the magic numbers or changing the structure!