Javascript's Math.random()
returns a psuedo-random number with "uniform" distribution.
I need to generate a random number in the range [0,1] that is skewed to either side. (Meaning, higher chance of getting more numbers next to 0 or next to 1)
Ideally I would like to have a parameter to set this curve.
I supposed I can do Math.random^2
to get such a result, but what more sophisticated ways are there to achieve this?
I think you want beta distribution with alpha=beta=0.5
It is possible to transform uniform random number to beta distribution using inverse cumulative distribution.
unif = Math.random()
I am not familiar with javascript
, but this should be clear:
beta = sin(unif*pi/2)^2
PS: you can generate many such numbers and plot histogram
Edit:
For skewing towards 0, transform the beta
values as -
beta_left = (beta < 0.5) ? 2*beta : 2*(1-beta);
For skewing towards 1, transform as -
beta_right = (beta > 0.5) ? 2*beta-1 : 2*(1-beta)-1;
I just came up with a simpler way to get random numbers skewed to each side, and which doesn't have any dependencies.
This method uses two of Javascript's regular random numbers. The first is multiplied with itself (the larger the exponent, the bigger the skewing effect), and the second chooses which side of the distribution to skew to.
function skewedRandom() {
const a = Math.pow(Math.random(), 2);
if (Math.random() < 0.5) {
return a;
}
return 1 - a;
}
In this code the exponent is set to 2. A sample histogram from 10,000 executions with the exponent at 2 as above:
With exponent 3:
And with exponent 10:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With