summaryrefslogtreecommitdiff
path: root/src/engine/utils/random.ts
blob: 3d9d8b8c52526e6cb82564baa52b10fa7d5835e2 (plain)
1
2
3
4
5
6
7
8
9
export const normalRandom = (mean: number, stdDev: number, maxStdDevs = 2) => {
  const [u, v] = [0, 0].map(() => Math.random() + 1e-12);
  const normal =
    mean + stdDev * Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
  return Math.min(
    mean + maxStdDevs * stdDev,
    Math.max(mean - maxStdDevs * stdDev, normal),
  );
};