The code seems to work. But you are not guaranteed to get the numbers generated you are looking for. You can try logging out the array of random numbers first to see what numbers you got.
Also, just as an aside, you should declare your variables in the loops.