math - Implementing probability distribution function in Java -
i'm trying implement probability distribution function in java returns ith
entry in array probability:
fi = 6i(n-i) / (n3 - n)
where n
array length i.e. array length 4:
p1 = 3/10, p2 = 4/10, p3 = 3/10, p4 = 0
note function assumes numbering 1 n
rather 0 n-1
in java.
at moment i'm using uniform distribution i.e.
int = (int)(math.random()*((arraysize)-1));
with -1 doesn't choose last element (i.e. pn = 0 in above formula).
anyone ideas or tips on implementing this?
double rand = math.random(); // generate random number in [0,1] f=0; // test if rand in [f(1)+..+f(i):f(1)+..+f(i)+f(i+1)] in rnge proba p(i) , therefore if in range return (int i=1,i<array.size();i++ ){ f+=f(i); if rand < f return i; } return array.size(); // went through array rand==1 (this probability null) , return n
Comments
Post a Comment