mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-21 12:05:50 +00:00
3c56638e8b
This was SVN commit r19109.
25 lines
534 B
JavaScript
25 lines
534 B
JavaScript
/**
|
|
* Returns a pair of independent and normally distributed (zero mean, variance 1) random numbers.
|
|
* Uses the Polar-Rejection method.
|
|
*/
|
|
function randomNormal2D()
|
|
{
|
|
let s, a, b;
|
|
do
|
|
{
|
|
a = 2 * Math.random() - 1;
|
|
b = 2 * Math.random() - 1;
|
|
s = a * a + b * b;
|
|
} while (s>=1 || s==0);
|
|
s = Math.sqrt(-2 * Math.log(s) / s);
|
|
return [a * s, b * s];
|
|
}
|
|
|
|
/**
|
|
* Return a random element of the source array
|
|
*/
|
|
function pickRandom(source)
|
|
{
|
|
return source.length ? source[Math.floor(source.length * Math.random())] : undefined;
|
|
}
|