Skip to content

Instantly share code, notes, and snippets.

@framp
Created January 12, 2023 07:37
Show Gist options
  • Save framp/70ef9a018df07e83e0b7254ef1581d14 to your computer and use it in GitHub Desktop.
Save framp/70ef9a018df07e83e0b7254ef1581d14 to your computer and use it in GitHub Desktop.
Newton-Rhapson-Method for nicely distributed random numbers on a 2d plane
const randomPoints = (n: number, d = 2) =>
const d = 2;
let g = 1.0;
for (let i = 0; i < 20; i++) {
g = g - (Math.pow(g, d + 1) - g - 1) / ((d + 1) * Math.pow(g, d) - 1);
}
const alpha = new Array(d).fill(0);
for (let j = 0; j < d; j++) {
alpha[j] = Math.pow(1 / g, j + 1) % 1;
}
const points = new Array(n).fill(0).map(() => new Array(d).fill(0));
for (let i = 0; i < n; i++) {
for (let j = 0; j < d; j++) {
points[i][j] = (0.5 + alpha[j] * (i + 1)) % 1;
}
}
return points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment