Possible Duplicate:
Generate random number with non-uniform density
I try to identify/create a function ( in Java ) that give me a nonuniform distributed sequence of number.
if I has a function that say function f(x), and x>0
it will give me a random number
from 0
to x
.
The function most work with any given x
and this below is only a example how I want to have.
But if we say x=100
the function f(x)
will return s nonunifrom distributed.
And I want for example say
0 to 20
be approximately 20% of all case.
21 to 50
be approximately 50% of all case.
51 to 70
be approximately 20% of all case.
71 to 100
be approximately 10 of all case.
In short somting that give me a number like normal distribution and it peek at 30-40 in this case x
is 100
.
http://en.wikipedia.org/wiki/Normal_distribution
( I can use a uniform random gen as score if need, and only a function that will transfrom the uniform result to a non-uniform result. )
EDIT
My final solution for this problem is:
/**
* Return a value from [0,1] and mean as 0.3, It give 10% of it is lower
* then 0.1. 5% is higher then 0.8 and 30% is in rang 0.25 to 0.45
*
* @return
*/
public double nextMyGaussian() {
double d = -1000;
while (d < -1.5) {
// RANDOMis Java's normal Random() class.
// The nextGaussian is normal give a value from -5 to +5?
d = RANDOM.nextGaussian() * 1.5;
}
if (d > 3.5d) {
return 1;
}
return ((d + 1.5) / 5);
}