# calculate win chance and win or not

I've got a logical problem with my mathematics skills. so far, I have to calculate a prizing system to say "you won" or "you lose".

here my way until now:

• u = max. users
• p = prizes
• w = winchance in percent: p*100/u

to say, if you won or not I use this:

$win = (rand(0,10000) > 100*(100-w)) ? TRUE : FALSE;  is this the correct way to calculate it? or is there still a better solution? thanks a lot, ~frank • Does max.users u=10000 ? – CAGT Jan 22 '14 at 8:31 • Minor problem for rand: if$w=100$, there is still slight chance for the code to give a FALSE. I suggest changing the rand part to rand(1,10000). Bigger problem, but maybe still minor: rand might be giving integers only, so if the right hand side of inequality$100\times(100-w)$is not an integer, the result may not be precise. – peterwhy Jan 22 '14 at 9:21 ## 1 Answer Apart from the problem of the random function I wrote above, the logical problem here is that the result from every run of the code is supposed to be independent. And so, if$w$is fixed and the code is run for$u$times, the number of wins follows binomial distribution: $$W\sim B\left(u,\frac pu\right)$$ And while the expected number of wins is$p$, there is quite some chance that the resultant number of wins is different from$p$. Actually, $$P[W\neq p] = 1 - \binom{u}{p}\left(\frac pu\right)^p\left(1-\frac pu\right)^{u-p}$$ If$p$is fixed and$u$approaches infinity, the distribution converges to Poisson distribution with expected number of wins$p$. The probability $$P[W\neq p]\approx 1-\frac{p^p}{p!}e^{-p}$$ For$p=1$and$u=10000$, there is over$63\%$chance that the number of wins is not equal to your$p$. • could you please explain this in "my words"? like I did with$win = .... cause my skills in mathematic syntax is to low to understand you. I've got the % of the winchance, so how can I say: you win or lose, based on the % winchance? – frank allen Jan 22 '14 at 9:52
• In short, you can almost keep your code $win=(rand(1,10000) > 100*(100-w)) ? TRUE : FALSE;, but the problem of this logic is that the number of wins after running the code for$u$times is not guaranteed to equal to$p$. – peterwhy Jan 22 '14 at 9:57 • in my example, I startet with 4 prizes and 10 users, after a few runnings, I had 3 prizes left for 3 users, so it's a 100% chance to win? – frank allen Jan 22 '14 at 10:03 • OK, if you modify$u$to "number of remaining users" and$p\$ to "number of remaining prices", that would be better. – peterwhy Jan 22 '14 at 10:08