Assuming that rn is in [j, m] inclusive, there are m - j + 1 numbers in this range; using basic modulo logic there are floor((m - j + 1) / j) of them which satisfy the if condition. Adopting a probabilistic approach and assuming that Random() is uniform, the total time complexity is given by:

Where P(m,j) is the probability of the if condition being satisfied - i.e. of the while-loop not executing, and S(o,p) is the time complexity of the while-loop. Since the inner loops are independent of i, j, we can treat them separately.
While it initially looks like the inner loops will require a similar analysis, there are two key differences to note:
b is never reset to true until after the while loop.
- The
if condition does not cause any of the loops to break.
- But most importantly,
b will always be false by the time the two for-loops break. Why? because when l = p, the only possible value for rn to take is also p; p % p = 0 so b will always be set to false if it hadn't already.
Therefore the inner while loop only executes up to once, and S(o,p) = O(op) trivially. Combining the two results, we get:

Summing over the 1 trivially gives O(mnop), but what about the P(m,j)? Using the fact that a number rounded down differs from its original value by less than 1:

Where in step (1) we did an index shifting transformation, and in step (2) used the asymptotic Harmonic series summation. Since this logarithmic term from the probabilistic analysis is overshadowed by the "bulk" term from before:
O(mnop) is therefore both the average and worst case complexity.
RandomFunction. In worst case, You may end up getting stuck in an infinite loop ifbnever becomesfalse.