skeeto / hash-prospector

Automated integer hash function discovery
The Unlicense
672 stars 27 forks source link

Usage in Path Tracing #15

Closed tay10r closed 3 years ago

tay10r commented 3 years ago

Or Monte Carlo simulations in general. I'm using this for a path tracer to generate coordinates within a pixel to take the next sample at. It's something like this:

struct vec2f { float x; float y; }

vec2f random_within_pixel(vec2f uv_min, vec2f uv_max, int seed) {
  int x = lowbias32(seed);
  int y = lowbias32(seed + 1);  // in the next call to this function: seed += 2
  float u = float(x) / std::numeric_limits<int>::max();
  float v = float(y) / std::numeric_limits<int>::max();
  return {
    uv_min.x + ((uv_max.x - uv_min.x) * u),
    uv_max.y + ((uv_max.y - uv_min.y) * v)
  };
}

The idea of using a hash instead of an RNG is not new for path tracing, Pixar does this with Correlated Multi-Jittered Sampling. Since I don't know much about the gory details of random number generation, I figured I'd get your take on whether or not this is a good use of lowbias32. Thanks in advance.