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.
I am not familiar with
javascript
, but this should be clear:PS: you can generate many such numbers and plot histogram
Edit:
For skewing towards 0, transform the
beta
values as -For skewing towards 1, transform as -
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.
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:
Yoy can use window.crypto.getRandomValues where available
On jsfiddle
(if that's what your asking for, I'm not sure)
Or maybe as this
also on jsfiddle
I think you need to rethink your question. The Poisson is a counting distribution specified in terms of a rate, such as how many occurrences of something do I see on average per time period. It yields positive integers so the result can't be just in the range [0,1]. Can you please clarify what you want?
Regardless, to generate a Poisson with rate lambda one algorithm is:
where "rand" is the function call for a Uniform(0,1). I don't know javascript, but that should be straightforward enough for you to implement.
Responding to edited question:
There are several distributions that generate outcomes on a bounded range, but many of them aren't for the faint of heart, such as the Johnson family or the Beta distribution.
An easy one would be triangle distributions. Sqrt(rand) will give a triangle distribution bunched towards 1, while (1-Sqrt(1-rand)) will give a triangle distribution bunched towards zero.
A more general triangle with the mode (most frequent value) at m (where 0 <= m <= 1) can be generated with
Note that each invocation of rand is a separate Uniform random number, this won't be correct if you generate one value for rand and use it throughout.