Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

[Note: I hope the title is appropriate. I am very much mathematically ignorant except when it comes to a limited scope of computer programming. Also, please excuse my lack of proper notation, and probably lack of sense in terminology and no knowledge of MathJaX. ]

If I have a loop over a limited set of iterations ($i$), let's say $i=10$

Upon each iteration, 3 random integers are generated, from 1-10, let's call them $a, b, c$ and a scoreboard is also started at 0, let's call them $x, y, z$ to coincide with $a, b, $c

Each iteration, on 3 separate occasions, there is a 10% (if random number $a, b, $ or $c$ = 1) chance that we will increment the corresponding counter $x, y,$ or $z$ by 1 and then restart the loop (without touching subsequent counters). Likewise, a 90% chance, that it will not increment and try the next condition (3 conditions total).

This would be better explained with some pseudo code:

int a, b, c = 0 # Holds a random number each iteration
int x, y, z = 0 # Holds a scoreboard throughout the iterations

for(int i = 0, i < 100000, i++) { # Iterate 100,000 times
  #All Separate Random Numbers
  a = rand(1,10) # Assuming this function returns a random integer between 1-10
  b = rand(1,10)
  c = rand(1,10)

  if(a == 1): x++; # 10% chance of increment
  else if (b == 1): y++; # 10% chance of increment if the 10% above fails
  else if (c == 1): z++; # 10% chance if both of the above 10%'s fail
}

It's a fairly simple concept in programming. Originally, I had thought, that $x$'s value would be about 10% of iterations, $y$'s value would be "90% of 10% of iterations" (or, 9% of course), and $z$'s value would be "90% of 9%'s of iterations" (or, 8.1%)

But, I ran this code in practice (using 100,000 iterations) and my results weren't way off, but they were off.

What I got was $x$ was about 10%, $z$ was about 8.1% (both correct), but $y$ seemed to fall short at about 8.9%. I ran it multiple times, and more often than not, I got 8.9%, not 9%

So the question is simply, how do I accurately predetermine probability in this situation?

To make matters even more complicated, what happens if you introduce another variable, let's call it $d$ that is a scoreboard for all iterations that were not present in $x, y$ or $z$?

I assume it will just be the leftover of probability from $x, y$ and $z$?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I tried the following Matlab code. Every time I ran this I got the expected values: viz, $x=10\%$, $y=9\%$ and $z\approx 8.1\%$. Notice that the number of iterations is higher in my case (1 million). So, if you increase you "number of tries" you will see that the simulation values will tend towards the theoretical values.

x=0;
y=0;
z=0;
iterations=1000000;
for k=1:iterations
    if(ceil(rand*10))==1
        x=x+1;
    elseif (ceil(rand*10))==1
        y=y+1;
    elseif (ceil(rand*10))==1
        z=z+1;
    end
end

x/iterations
y/iterations
z/iterations
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.