Hello.
I will not write the code for you. Actually thats one of the “funniest things on coding”
You want to get a result, nd want it by using random, so you dont know how many times will take for thes random function to give you the answer.
So, you need an infinite loop, an infinite loop that is broken whan the condition you need becomes true.
You are doing somethign like this inside the for (so you do this 5 times):
Generate number x using random
Generate Number y using random
if (some condition about x and y you dont want)
{
generate again x but NOT using random
generate again y but NOT using random
}
But, I ask you, maybe what you want to do is something like this:
Generate number x using random
Generate Number y using random
while (some condition about x and y you dont want)
{
generate again x but using random
generate again y but using random
}
This way, the code will be inside the while, until find a perfect solution.
BUT: it’s “dangerous” because it can take too miuch time (1 second) to find a result if the conditions are difficult to get, or you can even create a infinite while, where the code can’t go out…
So you can have a counter to prevent this infite loop, so for exampe, it only tries to get the numbers 20 times:
Generate number x using random
Generate Number y using random
int count = 0;
while (some condition about x and y you dont want && count < 20)
{
count ++;
generate again x but using random
generate again y but using random
}
I’m not sure if this is what you where asking for… 
Bye!