Skip to content

Instantly share code, notes, and snippets.

@ncruces
Last active December 3, 2019 13:36
Show Gist options
  • Save ncruces/b4ee29d37d2ea88a8775acb56a7a6975 to your computer and use it in GitHub Desktop.
Save ncruces/b4ee29d37d2ea88a8775acb56a7a6975 to your computer and use it in GitHub Desktop.
2D Hammersley low-discrepancy sequence
function hammersley(n, bias) {
if (isNaN(bias)) bias = (1-n)/(n+n);
function phi2(n) {
let r = 0;
let b = n.toString(2);
for (let i = 0; i < b.length; ++i) {
if (Number(b[i])) r += 1/(1 << b.length-i);
}
return r;
}
let r = [];
for (let i = 0; i < n; i++) {
r.push([i/n+bias, phi2(i)+bias]);
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment