From 8bc4b3e7fddc3a3fa93086ceb65761c9e9b7da25 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 2 Apr 2023 22:34:07 +0300 Subject: [PATCH] Add randomGauss --- snippets/randomGauss.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/randomGauss.md diff --git a/snippets/randomGauss.md b/snippets/randomGauss.md new file mode 100644 index 000000000..4cc8025f9 --- /dev/null +++ b/snippets/randomGauss.md @@ -0,0 +1,23 @@ +--- +title: Generate Gaussian random numbers +tags: math,random +cover: chess-pawns +author: chalarangelo +firstSeen: 2023-04-03T05:00:00-04:00 +--- + +Generates Gaussian (normally distributed) random numbers. + +- Use the [Box-Muller transform](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform) to generate random numbers with a Gaussian distribution. + +```js +const randomGauss = () => { + const theta = 2 * Math.PI * Math.random(); + const rho = Math.sqrt(-2 * Math.log(1 - Math.random())); + return (rho * Math.cos(theta)) / 10.0 + 0.5; +}; +``` + +```js +randomGauss(); // 0.5 +```